Magento 1.9 - 在“订单历史记录”页面中显示可配置的产品图像

时间:2018-05-15 11:59:41

标签: magento-1.9 configurable-product

我正在"我的订单"中显示订购产品的图像。在用户仪表板中。

<?php
        $orderId = $_order->getId(); 
        $order = Mage::getModel('sales/order')->load($orderId);
        $items = $order->getAllItems();
        foreach ($items as $itemId => $_item)
          {
            if($_item->getProductType() == 'configurable') 
                {
                  $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 }?>

一切正常。但它显示可配置产品的所有图像。我想显示所选的可配置简单产品图像。

任何帮助都将受到高度赞赏

2 个答案:

答案 0 :(得分:0)

使用以下代码:

<?php $orderId = $_order->getId(); ?>
<?php $order = Mage::getModel('sales/order')->load($orderId); ?>
<?php foreach ($order->getAllItems() as $itemId => $_item): ?>
    <?php if($_item->getProductType() == 'configurable')  :?>
      <?php $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku()); ?>
    <?php else: ?>
      <?php $product = Mage::getModel('catalog/product')->setStoreId($_item->getOrder()->getStoreId())->load($_item->getProductId()); ?>
    <?php endif; ?>
    <img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(75); ?>" width="75" height="75" />
<?php endforeach; ?>

希望这有帮助!

答案 1 :(得分:0)

我使用getAllVisibleItems代替getAllItems,现在它正在运作。

<?php
$orderId = $_order->getId(); 
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllVisibleItems();
foreach ($items as $itemId => $_item){
  if($_item->getProductType() == 'configurable'){
          $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 }?>