Magento - can't save changed customer GroupId with observer for newsletter_subscriber_save_before event -
i built observer newsletter_subscriber_save_before event;
gets fired when customer subscribes/unsubscribes newsletters. need change customers groupid accordingly. seems can change groupid, can't manage save it. if call in code $customer->save()
- method, run infinite loop; same occurs if try hook newsletter_subscriber_save_after
event.
anyone can me? appreciated!
here's code in observer.php :
public function newsletter_subscriber_change($observer) { try { // status of subscriber $subscriber = $observer->getevent()->getsubscriber(); $status = $subscriber->getstatus(); $email = $subscriber->getemail(); $customer = mage::getsingleton('customer/customer'); $customer->setwebsiteid(mage::app()->getwebsite()->getid()); $customer->loadbyemail($email); // here change groupid $customer->setdata( 'group_id', 9 ); // if check group here, change applied ... $customer_group = $customer->getgroupid(); // tried saving following, run loop $customer->save(); } catch (exception $e) { mage::log("newsletter_subscriber_change observer failed: " . $e->getmessage()); } }
you got infinite loop because mage_newsletter capture customer_save_after
event save subscribe data. can update group id raw query bellow:
public function newsletter_subscriber_change($observer) { try { // status of subscriber $subscriber = $observer->getevent()->getsubscriber(); $status = $subscriber->getstatus(); $email = $subscriber->getemail(); $customer = mage::getsingleton('customer/customer'); $customer->setwebsiteid(mage::app()->getwebsite()->getid()); $customer->loadbyemail($email); $adapter = mage::getsingleton('core/resource'); /* @var $conn varien_db_adapter_interface */ $conn = $adapter->getconnection('core_write'); $update = 'update ' . $customer->getresource()->gettable('customer/customer_enity') . ' set group_id = ? entity_id = ?'; $conn->query($update, array(9, $customer->getid())); } catch (exception $e) { mage::log("newsletter_subscriber_change observer failed: " . $e->getmessage()); } }
Comments
Post a Comment