Question

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');
Was it helpful?

Solution

In your observer, instead of

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

put

$address = $observer->getEvent()->getOrder()
                                  ->getShippingAddress();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top