Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top