Magento - customer_save_after always fired twice

前端 未结 5 1732
旧时难觅i
旧时难觅i 2020-12-08 22:36

I am using the customer_save_after event in magento, and all is working fine apart from 1 annoying thing - it is always fired twice.

There are no other

相关标签:
5条回答
  • 2020-12-08 22:55

    I've noticed this double-save behaviour too. The way to prevent issue with your observer is to set a flag in the request that can be checked e.g.

        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); 
    
    0 讨论(0)
  • 2020-12-08 22:58

    Be careful with Jonathans solution, 'customer_save_observer_executed' stays in the session, so event will not be fired again in the browser session. So it's generally a bad idea, because it will not allow to register two or more customers in a row(actually, it will, but events will not be fired)

    I suggest the following solution:

    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); 
    }  
    
    0 讨论(0)
  • 2020-12-08 22:58

    The difference between these 2 events is one of them can't get customer info, while the other can. So the solution is

        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
        }
    
    0 讨论(0)
  • 2020-12-08 23:02

    I ran into this as well and did a stack trace in the observer for each method, and can tell you at least ONE reason why it fires twice (there may be others):

    When a new user creates an account, createPostAction() runs when the form is submitted. This action does a save() on the customer.

    THEN, after the customer has been created, setCustomerAsLoggedIn() is called by createPostAction(). This in turn calls setCustomer(), which has this little bit of code:

     if ((!$customer->isConfirmationRequired()) && $customer->getConfirmation()) {
        $customer->setConfirmation(null)->save(); // here is the second save
        $customer->setIsJustConfirmed(true);
     }
    

    Those are the two save()s which dispatch the save event. I only know this for sure for account creation in Magento 1.5. I doubt if it gets fired twice when creating users in the Admin area, or when a user edit's their information... but I don't know for sure.

    I hope this helps!

    0 讨论(0)
  • 2020-12-08 23:03

    I used a static var:

    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++;
    }
    
    0 讨论(0)
提交回复
热议问题