skip to Main Content

I am displaying a image of ordered products in “My Order” in user dashboard.

<?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 }?>

All works fine. but it display all images of configurable products. i want to display the selected configurable simple product image.

Any help will highly appreciated

2

Answers


  1. Chosen as BEST ANSWER

    i used getAllVisibleItems instead of getAllItems and Now it is working.

    <?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 }?>
    

  2. Use the below code:

    <?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; ?>
    

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search