1

从观察者设置客户组 ID 时遇到问题。该事件正在通过 customer_register_success 事件创建新用户。该事件被传递给我的观察者,例如

    public function registrationSuccess(Varien_Event_Observer $observer) {

        // extract customer data from event
        $customer = $observer->getCustomer()->getData();

        Mage::log('COOKIES', json_encode($_COOKIE));

        // a cookie should have been set with the membership id
        if (isset($_COOKIE['membership_account_id'])) {

            Mage::log('COOKIE SET, ASSOCIATING MEMBERSHIP');

            // associate new account with membership, and upgrade user to membership status
            $this->associateMembership($customer['entity_id'], $_COOKIE['membership_account_id']);

        }

    }

然后调用 associateMembership 方法来更新组 ID,并设置一个名为 rms_id的自定义客户属性:

public function associateMembership($customer_id, $account_id) {

        // load customer model
        $customer = Mage::getModel('customer/customer')->load($customer_id);

        Mage::log('CUSTOMER DATA: ' . json_encode($customer->toArray()));

        // upgrade customer to membership level, and set custom rms_id attribute
        $customer
            ->setWebsiteId(Mage::app()->getWebsite()->getId())
            ->setGroupId(4)
            ->setRmsId($account_id);

        // save
        try {
            $customer->save();
            Mage::log('ACCOUNT ASSOCIATED: CUSTOMER ID = ' . $customer_id . ' ACCOUNT ID = ' . $account_id);
        } catch (Exception $ex) {
            Mage::log($ex);
        }

    }

由于某种原因,没有错误回来。我得到了正确的用户 ID,一切似乎都在工作。但是,未设置该组,我的自定义 ID 也未设置。

我应该使用另一个允许保存的事件吗?

4

2 回答 2

2

在加载客户之前尝试加载网站 ID

$customer = Mage::getModel('customer/customer')
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->load($customer_id);

customer_register_success将客户数据保存在自定义观察者中后将重新保存

还要customer_register_success传递客户数据,因此您不需要重新加载它。

见 /app/code/core/Mage/Customer/controllers/AccountController.php

Mage::dispatchEvent('customer_register_success',
    array('account_controller' => $this, 'customer' => $customer)
);

尝试

public function registrationSuccess(Varien_Event_Observer $observer) {

    // extract customer data from event
    $customer = $observer->getCustomer();

    Mage::log('COOKIES', json_encode($_COOKIE));

    // a cookie should have been set with the membership id
    if ($membership_account_id = Mage::getModel('core/cookie')->get('membership_account_id')) {
        Mage::log('COOKIE SET, ASSOCIATING MEMBERSHIP');

        $customer->setGroupId(4)
                 ->setRmsId($membership_account_id);
    }
    return $this;
}
于 2013-01-13T23:59:45.127 回答
0

尝试在加载前设置客户网站 ID。例如

$customer = Mage::getModel('customer/customer')
    ->setWebsiteId(Mage::app()->getWebsite()->getId())
    ->load($customer_id);

也尽量放死;在 $customer->save() 之后;在测试时 - 在这种情况下,您将确定在您之后没有其他任何东西会更改客户数据(可能是其他一些观察者)。

于 2013-01-13T23:54:20.010 回答