customer_register_success事件发生后,从观察者更新客户数据 [英] Updating customer data from observer after customer_register_success event

查看:109
本文介绍了customer_register_success事件发生后,从观察者更新客户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置观察者的客户组ID时遇到问题.该事件是通过customer_register_success事件在创建新用户时产生的.该事件已传递给我的观察者,例如

Having issues setting the customers group id from an observer. The event is picking up on a new user creation via customer_register_success event. The event is passed to my observer, e.g.

    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']);

        }

    }

然后调用该方法来更新组ID,并设置名为rms_id的 custom 客户属性:

Which then calls the associateMembership method to update the group id, and set a custom customer attribute called 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也未设置.

For some reason, there's no error coming back. I'm getting the correct user id, and everything seems to be working. However, the group is not being set, nor is my custom id.

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

Should I be using another event that will allow the save to go through?

推荐答案

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

Try loading the website id before loading the customer

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

customer_register_success将客户数据保存到自定义观察器后将重新保存

customer_register_success will re-save the customer data after you save it in your custom observer

customer_register_success传递客户数据,因此您无需重新加载它.

Also customer_register_success pass the customer data so you should not need to reload it.

请参阅/app/code/core/Mage/Customer/controllers/AccountController.php

see /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;
}

这篇关于customer_register_success事件发生后,从观察者更新客户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆