cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 1.9 - Based on region in US, Observer which can allocate customer group automatically

Magento 1.9 - Based on region in US, Observer which can allocate customer group automatically

I already implemented the observer that can allocate users to a customer group based on country after register. customer_register_success was used in this observer and it works well.

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.

 

Original Observer

 

<?php
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();
            }
        }
    }
}
?>

Btw, I'm trying to add some tweak for another purpose. On Magento setting, 'Puerto Rico'is independent country and one of province in United State as well. So i delete the 'Puerto Rico' on country name list and i'd like to manage customers who write 'Puerto Rico' as state/province like international customer because of USPS shipping issue!

 

So, this is a logic what i want to create :

 

585.JPG

 

I tried like this, but it doesn't work

 

<?php
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();
            $region = $billingAddress->getRegion(); 
            switch ($countryId) {
                case 'US':
                    if ($region == 'Puerto Rico'){
                        $customer->setData('group_id', 27);
                        $customer->save();
                    }else {
                        $customer->setData('group_id', 1);
                        $customer->save();
                    }
                    break;
                default:
                    $customer->setData('group_id', 27);
                    $customer->save();
            }
        }
    }
}
?>

But, I have no idea how to get region id, name from primary Billing address and What should i do? Please share your opinion. Thank you