Tuesday 5 May 2009

Item Options

Recently I was trying to debug some MadetoOrder (MTO Homepage) code and found a really odd behavior of the order item (Mage_Sales_Model_Order_Item). I was in the middle of working on a function which prints out special order options and found that

the functions you need to call to get the options ordered are dependent on whether or not the order is placed in the database or still in the customer's session.


//sometimes you call:
$item->getOptionsById()
//sometimes you call:
$item->getProductOptionsById()

//so you always have to do:
$buyRequest = $item->getProductOptionsById('info_buyRequest');
if ($buyRequest === NULL) {
$buyRequest = $item->getOptionsById('info_buyRequest');
}
if ($buyRequest === NULL) {
//do real error handling here, order item
//really doesn't have a buy request.
}



Which do you call when? It doesn't really matter, you have to do both all the time in one function and check for null, then call the other one. I was so horrified by my finding that I didn't bother checking which one was for the placed order and which ones was for the session.

So, if you want to change the way a product looks or deals with options, and you want your change to appear the same way in the "Your Cart" page as it does in the "Your Order History" page... you gotta use both for no apparent reason.

Don't believe me? Try it. How does Magento deal with this? Well, they don't really re-use code, so it doesn't affect them. The block for the shopping cart contains functions which are different than the block for the my account page.

No comments: