Magento:观察者方法getCustomer()为NULL



好的,我有一个观察controller_action_postdispatch_customer_account_createpost动作的观察者。我的问题是,在方法中,我试图做以下事情:

public function customerSaveAfter($observer)
{
    /** @var Mage_Customer_Model_Customer $customer */
    $customer = $observer->getEvent()->getCustomer();
}

无论我做什么,$customer都是NULL。在这之前还有另一个扩展被调用它以完全相同的方式使用那个方法并找到一个客户。请帮助。

客户对象为空白,因为controller_action_postdispatch_customer_account_createpost事件是控制器动作事件,与客户对象无关。该事件在以下代码

中发出
#File: app/code/core/Mage/Core/Controller/Varien/Action.php
public function postDispatch()
{
    if ($this->getFlag('', self::FLAG_NO_POST_DISPATCH)) {
        return;
    }
    Mage::dispatchEvent(
        'controller_action_postdispatch_'.$this->getFullActionName(),
        array('controller_action'=>$this)
    );
    Mage::dispatchEvent(
        'controller_action_postdispatch_'.$this->getRequest()->getRouteName(),
        array('controller_action'=>$this)
    );
    Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$this));
}

特别是

Mage::dispatchEvent(
    'controller_action_postdispatch_'.$this->getRequest()->getRouteName(),
    array('controller_action'=>$this)
);

。($this->getRequest()->getRouteName()返回customer_account_createpost)。请注意,

array('controller_action'=>$this)

被传递到事件调度中——这意味着你可以用下面的

从你的观察者访问控制器对象
$observer->getControllerAction();
$observer->getData('controller_action');

你也可以用

为观察者获取数据键变量列表
var_dump(
    array_keys($observer->getData())
);

"其他扩展"(我假设您指的是另一个扩展的观察者对象)可能正在监听一个不同的事件,一个将customer对象传递给事件的事件。例如,考虑customer_login事件。

#File: app/code/core/Customer/Model/Session.php
public function setCustomerAsLoggedIn($customer)
{
    $this->setCustomer($customer);
    Mage::dispatchEvent('customer_login', array('customer'=>$customer));
    return $this;
}

这里的事件调度包括一个客户对象

array('customer'=>$customer)

表示客户将在您的观察者中可用。

最新更新