1

我创建了一个自定义模块来向Mage/Checkout/Model/Type/Onepage.php中包含的 Checkout Model saveBilling()函数添加一个事件。除了每次调用事件都正确触发之外,模块现在偶尔会抛出错误:

Notice: Undefined property: Fla_Checkout_Model_Type_Onepage::$_customerEmailExistsMessage
in /home/magento/public_html/app/code/local/Fla/Checkout/Model/Type/Onepage.php
on line 154

模块的相关位,公共函数 saveBilling()中的代码除了dispatchEvent()行及其前面的注释外与 Magento 核心完全相同。引发错误的部分:

<?php
class Fla_Checkout_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage {
    public function saveBilling($data, $customerAddressId) {

        /* much code removed here as not relevant to question */

        /* This section throws error because $this->_customerEmailExistsMessage not found */


        if (!$this->getQuote()->getCustomerId() && self::METHOD_REGISTER == $this->getQuote()->getCheckoutMethod()) {
            if ($this->_customerEmailExists($address->getEmail(), Mage::app()->getWebsite()->getId())) {
                return array('error' => 1, 'message' => $this->_customerEmailExistsMessage);
            }
        }


        /* much code removed here as not relevant to question, below code works as intended*/

        /* Fiascolabs added event for exporting billing data */
        Mage::dispatchEvent('fla_billing_export', array('quote'=>$this->getQuote()));

        return array();
    }
}

当我查阅 Magento 核心代码时,在顶部,我声明了几个似乎在函数覆盖中无法访问的项目。

  1. 我的模块可以使用常量公共函数 __construct()吗?

  2. 我只需要复制申报的私人物品吗?

有问题的代码如下:

/**
 * Checkout types: Checkout as Guest, Register, Logged In Customer
 */
const METHOD_GUEST    = 'guest';
const METHOD_REGISTER = 'register';
const METHOD_CUSTOMER = 'customer';

/**
 * Error message of "customer already exists"
 *
 * @var string
 */
private $_customerEmailExistsMessage = '';

/**
 * Class constructor
 * Set customer already exists message
 */
public function __construct()
{
    $this->_helper = Mage::helper('checkout');
    $this->_customerEmailExistsMessage = $this->_helper->__('There is already a customer registered using this email address. Please login using this email address or enter a different email address to register your account.');
    $this->_checkoutSession = Mage::getSingleton('checkout/session');
    $this->_customerSession = Mage::getSingleton('customer/session');
}
4

1 回答 1

2

Magento 的类重写很容易被绊倒和迷惑,因为它们起初看起来像是巫术。然而,在“实例化这个类而不是这个其他类”功能之外,类重写只是标准的 PHP OOP,所有标准的 PHP OOP 规则都适用。

具体来说,如果您的类扩展了另一个类,它将无法访问基类/对象的私有属性。

private $_customerEmailExistsMessage = '';

您将有权访问受保护的和公共的属性和方法。如果你愿意,你可以重新定义__constructor方法

public function __construct()
{        
    //do the stuff in the parent consturctor
    parent::__construct();

    //do my new stuff
}    

你不需要打电话parent::__construct(),但除非你有理由不这样做,否则这样做总是一个好主意。该parent::methodName()语法也适用于其他方法

public function getCheckout()
{
    $parent_results = parent::getCheckout();

    //do my stuff

    return $parent_results;
}

常量有点棘手。虽然常量没有成员函数和变量之类的访问规则,但取决于您的 PHP 版本(5.2 与 5.3 及更高版本),PHP 的“后期静态绑定”行为将改变其特殊的“自我”、“父”和“静态”关键字与常量交互。一般来说,Magento 代码库在引用常量时使用完整的类名

echo Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER

我倾向于并建议您这样做。

于 2013-06-10T20:27:23.310 回答