2

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.

3
  • Your code is correct. Check if your module is enabled, also put a log entry on your observer method to check if it is being called Feb 17, 2017 at 21:26
  • @JaiminSutariya As i mention before, I'm too newbie on Magento. Could you let me know how to log this file by using mage::log or die (). I just heard about those methods but i don't get it how to log it. Where can i put the log command?
    – 3CE
    Feb 17, 2017 at 21:40
  • @JaiminSutariya i put Mage::log('customergroup', null, 'system.log', true); in Observer.php and after looking at the log file, it is written to "2017-02-17T22:00:23+00:00 DEBUG (7): customergroup" like this
    – 3CE
    Feb 17, 2017 at 22:03

2 Answers 2

2

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(); 
9
  • When i add your code,
    – 3CE
    Feb 20, 2017 at 14:11
  • Fatal error: Call to a member function getId() on a non-object in app/code/local/Kbethos/CustomerGrouped/Model/Observer.php on line 10
    – 3CE
    Feb 20, 2017 at 14:11
  • This error comes out
    – 3CE
    Feb 20, 2017 at 14:11
  • Try with updated code Feb 20, 2017 at 14:14
  • 1
    @zus, you will require to use $order->getAllItems() in a froeach loop to get each item object and then you can get product details by $item->getSku(), etc. Aug 22, 2019 at 12:42
1

Try to register even as shown below:

<events>
        <customer_register_success>
            <observers>
                <customergrouped>
                    <type>singleton</type>
                    <class>Kbethos_CustomerGrouped_Model_Observer</class>
                    <method>customerRegisterSuccess</method>
                </customergrouped>
            </observers>
        </customer_register_success>
</events>
1
  • I am using sales_order_place_after, how to know what are the details enclosed like name, price, qty_ordered, etc @Prasanta Hatui
    – zus
    Aug 22, 2019 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.