Observer Checkout Success – Change Customer Group Based on Country

checkoutcustomercustomer-groupevent-observer

Is this possible to get customer country (or country code) in checkout success observer ?
I would like to create a custom module (it's okay) to change customer ID switch his country after he orders.

Is this possible to get customer country in my Observer.php ?

Thanks a lot

Best Answer

You need to write below code in config.xml File: app/code/local/Letsknowit/Postdata/Checkout/etc/config.xml

   <config>
        <global>
           <models>
             <postdata>
                <class>Letsknowit_Postdata_Model</class>
              </postdata>
           </models>
        <events>
          <checkout_onepage_controller_success_action>
           <observers>
             <postDataTosServer>
                <type>singleton</type>
                  <class>Letsknowit_Postdata_Model_Checkout_Observer</class>
                  <method>postDataTosServer</method>
            </postDataTosServer>
          </observers>
         </checkout_onepage_controller_success_action>
      </events>    
   </global>   
 </config>

You need to write below code in Observer

File: app/code/local/Letsknowit/Postdata/Model/Checkout/Observer.php

<?php 
class Letsknowit_Postdata_Model_Checkout_Observer 
{
     public function postDataTosServer(Varien_Event_Observer $observer){
        $orderIds = $observer->getData('order_ids');
         foreach($orderIds as $_orderId){
            $order     = Mage::getModel('sales/order')->load($_orderId);
            $customer  = Mage::getModel('customer/customer')->load($order->getData('customer_id'));
            $customer->getDefaultBillingAddress()->getLastname();
           $billingaddress = $order->getBillingAddress();
           $countryCode = $billingaddress->getData('country_id');
           /* update and save user contry id */
           $countryId = 1 ;// put here desired country id
           $customer->setCountryId($countryId);
           $customer->save();




     }
 }   

 ?>
Related Topic