cancel
Showing results for 
Search instead for 
Did you mean: 

How to assign customer group automatically following by address information

SOLVED

How to assign customer group automatically following by address information

How to assign customer group automatically following by address information

When customer register on our website, customer write the country where they live. If customer live in United State, we would set the standard group(just default group). If not like live in Jamaica, Spain, we'd like to assign to international group automatically.

 

So, it would show like this,

hihihi.JPG

 

It seems like if-else statement on any languages but i didn't touch the code because i fix it myself one by one.

How can i do it? app/code/core/Mage/Customer/Model have the information about that i guess.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to assign customer group automatically following by address information

Hi @xzxzxzxyy

 

you have to check country for the customer's default billing address:

 

class aaa_bbb_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();
            }
        }
    }
}

Cheers,

BX

View solution in original post

6 REPLIES 6

Re: How to assign customer group automatically following by address information

@xzxzxzxyy In order to achieve that I guess its better to use Observer and track customer save event. let me help you more!

Use event : 

customer_register_success

And save from observer by:

/** get customer object */
$customer = $observer->getCustomer();

/** save group  id*/
$customer->setData('group_id',<group_id>);
$customer->save();

hope this help! cheers 

 

 

-
Magento Programmer | Was my answer helpful? You can accept it as a solution.

Re: How to assign customer group automatically following by address information

I just create this observer.php file. 

And add some if-else state like this.

I tried many times. Could you check my sort of condition statements?

 

 

Version 1)

<?php
class aaa_bbb_Model_Observer {
  public function customerRegisterSuccess(Varien_Event_Observer $observer) {
      $event = $observer->getEvent();
      $customer = $event->getCustomer();

      
      if( $country_id !== 'US'){ // if country is not US
          $customer->setData('group_id',27); //set groupid to 27(international group)
          $customer->save();
      }
}
}

version 2) 

<?php class aaa_bbb_Model_Observer { public function customerRegisterSuccess(Varien_Event_Observer $observer) { $event = $observer->getEvent(); $customer = $event->getCustomer(); $countryName == 'United States'; $countries = Mage::app()->getLocale()->getCountryTranslationList(); $countryCode = array_search($countryName, $countries); if($countryCode){ $customer->setData('group_id',1); //set groupid to 1(standard) $customer->save(); } } }

Version 3)

 
<?php
class aaa_bbb_Model_Observer {
  public function customerRegisterSuccess(Varien_Event_Observer $observer) {
$countryName = Mage::getModel('directory/country')->getCollection()->loadByCode($countryCode)->getName();

switch($countryName){
case "United States" :
$customer->setData('group_id',1); //set groupid to 1(standard)
$customer->save();
break;
default : $customer->setData('group_id',27); //set groupid to 27(international)
$customer->save();
break;
}
}
}

 

They didn't work tho.

huh 

Re: How to assign customer group automatically following by address information

Hi @xzxzxzxyy

 

you have to check country for the customer's default billing address:

 

class aaa_bbb_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();
            }
        }
    }
}

Cheers,

BX

Re: How to assign customer group automatically following by address information

But if you use this code, 

you add this line below line 5,

 

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

Re: How to assign customer group automatically following by address information

Hi @xzxzxzxyy

 

what for? The customer object is passed along with the event. There's no need to re-load it.

 

Cheers,

BX

Re: How to assign customer group automatically following by address information

No i tried your own code but 

I faced this error

 

Fatal error: Call to a member function getId() on a non-object in app/code/local/Kbethos/CustomerGrouped/Model/Observer.php on line 10


So i put code i attached. It works! 

the issue is related to get billing information of the customer.