2

In Magento 1.9, the "dashboard" and "order history" pages contain table of orders with summary info (date, order #, total) but not some helpful details (product image, product name, line price, quantity).

UX would be improved by highlighting the products in each order so the list of orders is more visual.

— — — — — — — — — —

Q: how can we load the product data for each order in the loop of orders without harming performance?

— — — — — — — — — —

Relevant files:

/sales/order/history.phtml
/sales/order/recent.phtml

Relevant code:

<?php
$orders = $this->getOrders();

foreach ($orders as $order) {
    echo $order->getRealOrderId();
    echo $order->getStatusLabel();
    // [todo] get products in order (name, image, url, price paid)
}
?>

Note: I'm a frontend developer, so please bear with me. A little more detail than just override X class or use Y model is helpful.

1

Heres a straightforward way that will do the trick. It's a bit messy since the info you need requires getting not just the order items but the corresponding products as well. I suppose that's Magento though.

<?php 
$orders = $this->getOrders();

foreach ($orders as $order) {
    /* @var $order Mage_Sales_Model_Order */
    $items = $order->getAllVisibleItems();
    $product_info = [];
    foreach ($items as $item) {
        /* @var $item Mage_Sales_Model_Order_Item */
        /* @var $product Mage_Catalog_Model_Product */
        $product = $item->getProduct();
        $product_info[] = [
            'name'  => $item->getName(),
            'price' => Mage::helper('core')->currency($item->getPrice(), true, false),
            'url'   => $product->getProductUrl(),
            'image' => $this->helper('catalog/image')->init($product, 'small_image')->resize(75, 75),
        ];
    }
    var_dump($product_info);
}

?>

This is probably not the most performance-friendly way though. With a large list of orders, I expect this could slow down page load. Hard to say until you test it. If better performance is needed, I'd look at extending these two block classes:

Mage_Sales_Block_Order_History
Mage_Sales_Block_Order_Recent

Override the constructor methods since that's where the order collections are originally loaded. A collection of Order Items and Products can be loaded there and attached to the Orders collection. This would be more efficient than loading order items and products for each order in a loop.

3
  • This is perfect! Making a couple edits so it's a more functional response. $order->getAllItems() changed to $order->getAllVisibleItems(). Add price formatter. Added image URL via helper. – Brendan Falkowski Sep 12 '17 at 22:12
  • Performance seems fine. A user would never see more than 10 orders per page. Might be slower if the orders have a huge amount of products, but not crazy slow to avoid it. Thanks! – Brendan Falkowski Sep 12 '17 at 22:15
  • Glad it fits the bill! No sense over optimizing if it's not needed. Those helper methods are definitely the proper way to go too. – Zeke Farwell Sep 13 '17 at 0:54

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.