Magento 1.9 – How to Create a New Observer on ‘customer_register_success’ Event

addressevent-observermagento-1.9

I'm quite newbie in magento. I'm trying to create observer on the event "customer_register_success". I follow the tutorial about how to compose the files in local module. But it didn't work.

When customer register on our website, customer should select the country where they live and it is saved as billing address. The goal i want to meet is that if customer live in United State, their group is standard group(just default group). If not, like live in Jamaica, Spain, they are assigned to international group automatically.

I know that GROUP exist because of tax class tho. but i must make it.

our website's customer group information is,
enter image description here

I'm willing to set the groupid based on their country name,id.
As you see the following,

1)Directory

enter image description here

2)app/etc/modules/Kbethos_CustomerGrouped.xml

First of all, I added a module configuration file to the app/etc/modules directory.

<?xml version="1.0"?>
<config>
    <modules>
    <Kbethos_CustomerGrouped>
        <active>true</active>
        <codePool>local</codePool>
    </Kbethos_CustomerGrouped>
    </modules>
</config>

3) app/code/local/Kbethos/CustomerGrouped/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Kbethos_CustomerGrouped>
            <version>0.1.0</version>
        </Kbethos_CustomerGrouped>
    </modules>
    <global>
        <helpers>
            <customergrouped>
                <class>Kbethos_CustomerGrouped_Helper</class>
            </customergrouped>
        </helpers>
        <models>
            <kbethos_customergrouped>
                <class>Kbethos_CustomerGrouped_Model</class>
            </kbethos_customergrouped>
        </models>
        <events>
            <customer_register_success>
                <observers>
                    <kbethos_customergrouped>
                        <class>kbethos_customergrouped/observer</class>
                        <method>customerRegisterSuccess</method>
                    </kbethos_customergrouped>
                </observers>
            </customer_register_success>
    </events>
    </global> 
</config>

4)app/code/local/Kbethos/CustomerGrouped/Model/Observer.php

<?php
  class Kbethos_CustomerGrouped_Model_Observer {
  public function customerRegisterSuccess(Varien_Event_Observer $observer) {

 $event = $observer->getEvent();
    $customer = $event->getCustomer();
    $billingAddress = $customer->getPrimaryBillingAddress();

    if ($billingAddress) {
        $countryId = $billingAddress->getCountryId();
        switch ($countryId) {
            case 'US':
                $customer->setData('group_id', 1);
                $customer->save();
                break;
            default:
                $customer->setData('group_id', 27);
                $customer->save();
        }
    }

}
}


?>

5) /wh-clone/app/code/local/Kbethos/CustomerGrouped/Helper/Data.php

<?php 

class Kbethos_CustomerGrouped_Helper_Data extends Mage_Core_Helper_Abstract
{
}

What sould i do more? please let me know. Thank you.

Best Answer

If you are getting message in log then your observer is being called. There is no issue there.
So, now the issue is related to get billing information of the customer. Try loading customer before you get customer billing address.

class Kbethos_CustomerGrouped_Model_Observer {
    public function customerRegisterSuccess(Varien_Event_Observer $observer) {
        $event = $observer->getEvent();
        $customer = $event->getCustomer();
        $customer = Mage::getModel('customer/customer')->load($customer->getId());
        $billingAddress = $customer->getPrimaryBillingAddress();
        if ($billingAddress) {
            $countryId = $billingAddress->getCountryId();
            switch ($countryId) {
                case 'US':
                    $customer->setData('group_id', 1);
                    $customer->save();
                    break;
                default:
                    $customer->setData('group_id', 27);
                    $customer->save();
            }
        }
    }
}

Or you can also use

$billingAddress = $customer->getDefaultBillingAddress(); 
Related Topic