dongxu3029 2012-12-11 07:55
浏览 41
已采纳

我们怎样才能覆盖Magento中的抽象类?

I want to Override an abstract class of magento. ie Mage_Customer_Model_Address_Abstract. My intention is to get rid of Telephone number validation.

I tried copying app/code/core/Mage/Customer/Model/Address/Abstract.php

To

app/code/local/Telephone/Customer/Model/Address/Abstract.php

Tried to Override the function validate() with new code.

public function validate()
    {
        $errors = array();
        $this->implodeStreetAddress();
        if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the first name.');
        }

        if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the last name.');
        }

        if (!Zend_Validate::is($this->getStreet(1), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the street.');
        }

        if (!Zend_Validate::is($this->getCity(), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the city.');
        }

       /* if (!Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the phone number.');
        } */

        $_havingOptionalZip = Mage::helper('directory')->getCountriesWithOptionalZip();
        if (!in_array($this->getCountryId(), $_havingOptionalZip)
            && !Zend_Validate::is($this->getPostcode(), 'NotEmpty')
        ) {
            $errors[] = Mage::helper('customer')->__('Please enter the zip/postal code.');
        }

        if (!Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
            $errors[] = Mage::helper('customer')->__('Please enter the country.');
        }

        if ($this->getCountryModel()->getRegionCollection()->getSize()
               && !Zend_Validate::is($this->getRegionId(), 'NotEmpty')
               && Mage::helper('directory')->isRegionRequired($this->getCountryId())
        ) {
            $errors[] = Mage::helper('customer')->__('Please enter the state/province.');
        }

        if (empty($errors) || $this->getShouldIgnoreValidation()) {
            return true;
        }
        return $errors;
    }

But, I cant make it work!

Only way i can do it is by exactly copying the file to local folder. ie

app/code/core/Mage/Customer/Model/Address/Abstract.php

To

app/code/local/Mage/Customer/Model/Address/Abstract.php

and my config.xml in app/code/local/Customer/etc/ is..

<?xml version="1.0"?>
<config>
    <modules>
        <Telephone_Customer>
            <version>0.0.1</version>
        </Telephone_Customer>
    </modules>

    <global>
    <models>
    <telephone_customer>
        <class>Telephone_Customer_Model</class>
    </telephone_customer>
    <customer>
        <rewrite>
            <customer>Telephone_Customer_Model_Customer</customer>
        </rewrite>
    <customer>
    </models>
    </global>   
</config>

But it is not allowed here bcz its not the right way to do it.

Can we override this abstract class like what we do in elsewhere..?

Please help.

  • 写回答

1条回答 默认 最新

  • duanbi1983 2012-12-11 10:12
    关注

    Got it Finally.

    I can access the

    function validate()

    overriding derived classes such as Address.php.

    ie overriding

    app/code/core/Mage/Customer/Address.php

    Same principles we can use as i described in my code above to override the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题