0

I want to prevent a fatal error if object is not available.

Background: I wrote an observer to catch data from the shipping object.

$o = Mage::getModel('sales/order')->load($observer->getOrderIds());
    if (is_object($o->getShippingAddress()->getPostcode()))
        $zip = $o->getShippingAddress()->getPostcode()

But I get an fatal error if the Shipping Address object will not be loaded (i.e. for downloadable products).

Fatal error: Call to a member function getPostcode() on a non-object in /var/www/app/code/local/MyModule/Model/Observer.php on line 22

How can I get rid of it?

Thank you so much

1

Try it like this.

$o = Mage::getModel('sales/order')->load($observer->getOrderIds());
if ($o->getShippingAddress() && $o->getShippingAddress()->getPostcode()) {
    $zip = $o->getShippingAddress()->getPostcode();
}
|improve this answer|||||

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.