Php Magento-客户_保存_后始终触发两次

Php Magento-客户_保存_后始终触发两次,php,events,magento,observer-pattern,Php,Events,Magento,Observer Pattern,在magento中,我正在使用customer\u save\u after事件,除了一件恼人的事情外,一切正常-它总是被触发两次 没有其他模块重写这个,我也找不到发生这种情况的其他原因。当我查看所有在这个时候被解雇的事件时,这个事件肯定会被解雇两次 有人解释这个吗 我正在编写一个与此挂钩的web服务,它的复制效率非常低。我也遇到了这个问题,并在observer中对每个方法进行了堆栈跟踪,并且可以告诉您至少一个它触发两次的原因(可能还有其他原因): 当新用户创建帐户时,createPostAct

在magento中,我正在使用
customer\u save\u after
事件,除了一件恼人的事情外,一切正常-它总是被触发两次

没有其他模块重写这个,我也找不到发生这种情况的其他原因。当我查看所有在这个时候被解雇的事件时,这个事件肯定会被解雇两次

有人解释这个吗


我正在编写一个与此挂钩的web服务,它的复制效率非常低。

我也遇到了这个问题,并在observer中对每个方法进行了堆栈跟踪,并且可以告诉您至少一个它触发两次的原因(可能还有其他原因):

当新用户创建帐户时,createPostAction()将在提交表单时运行。此操作对客户执行
save()

然后,在创建客户之后,createPostAction()调用setCustomerAlloggedin()。这将依次调用setCustomer(),其中包含以下代码:

 if ((!$customer->isConfirmationRequired()) && $customer->getConfirmation()) {
    $customer->setConfirmation(null)->save(); // here is the second save
    $customer->setIsJustConfirmed(true);
 }
这两个save()用于分派save事件。我只知道在Magento 1.5中创建帐户时可以肯定这一点。我怀疑当在管理区创建用户时,或者当用户编辑他们的信息时,它是否会被触发两次。。。但我不确定


我希望这有帮助

我也注意到这种双重保存行为。防止观察员出现问题的方法是在请求中设置一个可检查的标志,例如

    if(Mage::registry('customer_save_observer_executed')){
        return $this; //this method has already been executed once in this request (see comment below)
    }

    ...execute arbitrary code here....

    /* Customer Addresses seem to call the before_save event twice, 
    * so we need to set a variable so we only process it once, otherwise we get duplicates
    */
    Mage::register('customer_save_observer_executed',true); 

使用Jonathans解决方案时要小心,“customer\u save\u observer\u executed”保留在会话中,因此不会在浏览器会话中再次触发事件。因此,这通常是一个坏主意,因为它不允许连续注册两个或多个客户(实际上是这样,但不会触发事件)

我建议采取以下解决办法:

public function customerRegister(Varien_Event_Observer $observer)
{       
    $customer = $observer->getEvent()->getCustomer();          
    if (!$customer->getId())
        return $this;                

    if(Mage::registry('customer_save_observer_executed_'.$customer->getId()))
        return $this;  

    //your code goes here

    Mage::register('customer_save_observer_executed_'.$customer->getId(),true); 
}  
我使用了一个静态变量:

private static $_handleCustomerFirstSearchCounter = 1;

public function Savest($observer) {
    if (self::$_handleCustomerFirstSearchCounter > 1) {
        return $this;
    }

    $customerData = Mage::getSingleton('customer/session')->getCustomer();
    $model = Mage::getModel('customerst/customerst')
        ->setQueryText(Mage::app()->getRequest()->getParam('q'))
        ->setCustomerId($customerData->getId())
        ->setCustomerName($customerData->getName())
        ->save();

    self::$_handleCustomerFirstSearchCounter++;
}

这两个事件之间的区别是其中一个无法获取客户信息,而另一个可以。所以解决办法是

    public function email_CustomerRegister(Varien_Event_Observer $observer){

        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $customer_email                 = $customer->getEmail();


        if(empty($customer_email)){
            return;
        }

        // do something
    }

感谢您的检测工作,这应该被标记为一个答案。实际上,customer_register_success事件更适合观察客户注册。是的,但是,当用户在帐户仪表板上保存时,customer_register_success不会触发。@VladislavMosalsky注册表项仅针对该请求存在,它们不会停留在会话中(参考)