Thursday, May 10, 2012

Accessing custom options of an order in Magento via PHP


I'm loading an order like this:




$order = Mage::getModel('sales/order')->load(2886);
$items = $order->getAllItems();



Then I use a foreach loop:




foreach ($items as $itemId => $item){
$name[] = $item->getName();
$unitPrice[]=$item->getPrice();
$sku[]=$item->getSku();
$ids[]=$item->getProductId();
$qty[]=$item->getQtyToInvoice();
}



And I am able to get most of the data I need. However, I'm having problems getting the custom options that were selected for the order. I can see the data in a var dump, but I have had no success in mining it out. I've also tried a handful of built in functions that I found via google, but no luck.


Source: Tips4all

1 comment:

  1. matt (OP) already self-answered the question.

    Quote:



    EDIT: Found it!

    I was able to get what I needed by using:

    $opts = $item->getProductOptions();


    Within my foreach loop. A var_dump on that shows how to access the data easily.



    Note: It's absolutely OK to self-answer your own question. Please just post it as an real answer, but not in a question or comment. Posting as real answer helps to keep the "Unanswered" list more clear (avoids making other people wasting their time). Thank you.

    ReplyDelete