1

I am using CE 1.9.2.2 configured for multi website.

I am trying to resolve an error in a shipping module which restricts certain pincodes.

The module works fine on frontend side but unfortunately it fails if order is created from the admin panel or if I try to add comments to an order.

After bit of debugging I found out that the observer fails to get the postcode from shipping address.

Config.xml

<events>
  <sales_order_save_before>
    <observers>
      <sales_order_save_before_handler>
        <type>model</type>
        <class>shippingrestriction/observer</class>
        <method>Shippingzip</method>
        <args></args>
      </sales_order_save_before_handler>
    </observers>
  </sales_order_save_before>
</events>

Observer.php

public function Shippingzip(Varien_Event_Observer $observer)
        {
            $collection = Mage::getModel('shippingrestriction/shippingzip')->getCollection();

            $data = array();
            foreach ($collection as $value)
            {
                $data[] = $value->zipcode;
            }
            $address = Mage::getSingleton('checkout/session')->getQuote()
                                  ->getShippingAddress();   

            if (in_array($address->getData('postcode'),$data))
            {
                throw new Mage_Core_Exception(
                Mage::helper('catalog')->__('Shipping is not available for your location.')
                );die;
            }
            else
            {
            }

        }

While debugging I found out that the below function returns empty value in the admin panel create order page and thus the postcode comparison code in the observer file fails in the admin panel.

$address =  Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
$address->getData('postcode');
1

In your observer, instead of

$address = Mage::getSingleton('checkout/session')->getQuote()
                                  ->getShippingAddress(); 

put

$address = $observer->getEvent()->getOrder()
                                  ->getShippingAddress();
  • Hi, First of all thank you for replying. It worked without any problem. Was it because of ('checkout/session')? – danish Mar 1 '16 at 8:55
  • yes :D... checkout session is gone in admin area and you need to take the shipping information from another source. – Alex Constantinescu Mar 1 '16 at 17:24
  • Ok. I am basically a newbie to magento and I don't seem to find much docs about magento functions, events or models or anything. Is there something you can suggest me? – danish Mar 2 '16 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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