Magento - getting data from an order or invoice

12,632

Solution 1

Many Magento objects are based on Varien_Object, which has a method called getData() to get just the usually interesting data of the object (excluding the tons of other, but mostly useless data).

With your code you could either go for all the data at once:

$shipping_address = $order->getShippingAddress();
var_dump($shipping_address->getData());

or directly for specific single properties like this:

$shipping_address = $order->getShippingAddress();
var_dump(
    $shipping_address->getFirstname(),
    $shipping_address->getLastname(),
    $shipping_address->getCity()
);

To understand how this works, I'd recommend to make yourself more familiar with the Varien_Object and read a bit about PHPs magic methods, like __call(), __get() and __set().

Solution 2

Try print_r($shipping_address->toArray());

Share:
12,632
gregdev
Author by

gregdev

Web and Android App developer

Updated on June 05, 2022

Comments

  • gregdev
    gregdev almost 2 years

    I'm trying to write a Magento (CE 1.4) extension to export order data once an order has been paid for. I’ve set up an observer that hooks in to the sales_order_invoice_save_after event, and that is working properly - my function gets executed when an invoice is generated. But I’m having trouble getting information about the order, such as the shipping address, billing address, items ordered, order total, etc.

    This is my attempt:

    class Lightbulb_Blastramp_Model_Observer {
        public function sendOrderToBlastramp(Varien_Event_Observer $observer) {
            $invoice = $observer->getEvent()->getInvoice();
            $order = $invoice->getOrder();
    
            $shipping_address = $order->getShippingAddress();
            $billing_address = $order->getBillingAddress();
            $items = $invoice->getAllItems();
            $total = $invoice->getGrandTotal();
    
            return $this;
        }
    }
    

    I tried doing a print_r on all those variables, and ended up getting a lot of data back. Could someone point me in the right direction of getting the shipping address of an order?

    Thanks!

  • gregdev
    gregdev over 12 years
    Thanks so much, this is exactly what I need.