1

我建立了一个观察者,newsletter_subscriber_save_before event;当客户订阅/取消订阅时事通讯时,它会被触发。我需要相应地更改此客户 groupID。看来我可以更改 GroupId,但我无法保存它。如果我在我的代码中调用$customer->save()- 方法,我会陷入无限循环;如果我尝试加入newsletter_subscriber_save_after事件,也会发生同样的情况。

任何人都可以帮助我吗?将不胜感激!

这是我在 Observer.php 中的代码:

public function newsletter_subscriber_change($observer) {
    try {

        // get 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 i change the groupId
        $customer->setData( 'group_id', 9 );
    // if i check the group here, the change applied ...
        $customer_group = $customer->getGroupId();

        // i tried saving with following, but run into loop
        $customer->save(); 

    } catch (Exception $e) {
        Mage::log("newsletter_subscriber_change observer failed: " . $e->getMessage());
    }
}
4

1 回答 1

0

You got infinite loop because Mage_Newsletter also capture customer_save_after event to save subscribe data. You can update group id by raw query like bellow:

public function newsletter_subscriber_change($observer) {
    try {

        // get 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 = ? WHERE entity_id = ?';

        $conn->query($update, array(9, $customer->getId()));

    } catch (Exception $e) {
        Mage::log("newsletter_subscriber_change observer failed: " . $e->getMessage());
    }
}
于 2013-05-08T16:11:54.657 回答