Magento 1.9:在订单历史页面显示所选可配置商品的关联简单商品图片
Fixing Configurable Product Image Display in Magento Order History
Hey there! I get exactly what you're dealing with—right now your code is pulling the main image from the configurable parent product instead of showing the specific simple variant the customer actually purchased. Let's adjust your code to fix that.
Modified Code
Here's the updated snippet that will display the correct simple product image for configurable orders:
<?php $orderId = $_order->getId(); $order = Mage::getModel('sales/order')->load($orderId); $items = $order->getAllItems(); foreach ($items as $itemId => $_item) { // Skip child items to avoid duplicate displays (we'll handle them via the parent configurable item) if ($_item->getParentItemId()) { continue; } $product = null; if($_item->getProductType() == 'configurable') { // Fetch the associated simple product item from the configurable order entry $childItems = $_item->getChildrenItems(); if (!empty($childItems)) { // Grab the first (and only) child item—each configurable order item maps to one specific variant $childItem = reset($childItems); $product = Mage::getModel('catalog/product') ->setStoreId($_item->getOrder()->getStoreId()) ->load($childItem->getProductId()); } else { // Fallback to parent product if no child item is found (safety net) $product = $_item->getProduct(); } } else { $product = Mage::getModel('catalog/product') ->setStoreId($_item->getOrder()->getStoreId()) ->load($_item->getProductId()); } ?> <img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(75); ?>" width="75" height="75" /> <?php }?>
Key Changes Explained
- Skip Child Items: We added a check to skip items that have a parent ID. This prevents duplicate images since the configurable parent item already links to its corresponding simple variant.
- Fetch the Correct Simple Product: For configurable items, we use
getChildrenItems()to pull the actual simple product item that was ordered. Loading this product gives us the exact image the customer saw when they made their purchase. - Fallback Safety: If for some reason the child item isn't found (an edge case), we fall back to the parent product image to avoid broken images on the page.
Alternative Approach (Using Product Options)
If you prefer another method, you can fetch the simple product directly via the order item's options:
// Inside the configurable product check $simpleProductId = $_item->getProductOptionByCode('simple_product'); if ($simpleProductId) { $product = Mage::getModel('catalog/product') ->setStoreId($_item->getOrder()->getStoreId()) ->load($simpleProductId); }
The first method using getChildrenItems() is generally more reliable since it directly references the order's line items.
内容的提问来源于stack exchange,提问作者Amir Iqbal
相关产品推荐
相关产品推荐

