1

I have been trying to work this out for the past few hours. Hope you guys can help. I am working on a magento website 1.9.0.1 and trying to display the products currently in the cart as a widget elsewhere. I am able to display the Product Name, Price, qty and the grand total. However I can not get the product image to display. Instead of the product image the default magento placeholder image is displayed.

This is the code block that i am working with :

<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
    <div class="cart-content-image">
        <img src="<?php echo Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
    </div>
    <div class="cart-content-details">
        <?php 
            $maxLength = 50;
            $productName = $item->getName();
            echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
            echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
            echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
            echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>'; 
        ?>
    </div>
</div>
<?php }
?>

The problem I am having is on this line:

<img src="<?php echo Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />

I have searched for the past few hours, and tried different solutions, but couldn't get them to work, so I thought I'd display the code in question.

Help and advise appreciated. Thanks

2
  • "Instead of the product image the default magento placeholder image is displayed" make sure you have assigned thumbnail image to that product Commented Dec 7, 2015 at 5:06
  • @MineshPatel Thanks for your input. Tried that at the first instance but of no help in this case.
    – AlphaOne
    Commented Dec 7, 2015 at 10:34

2 Answers 2

1

Try below code:

<img src="<?php echo (string)Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail')->resize(50); ?>" alt="<?php echo $item->getProduct()->getName(); ?>" />
1
  • Thanks for your input, but still the same showing the default magento thumbnail.
    – AlphaOne
    Commented Dec 7, 2015 at 10:35
1

Ok finally got this to work. Did more look around on the net and thanks to @Robbie Averill from stackoverflow for giving me some ideas. The problem I was having is that the images I needed to display were of products already in the cart so using:

$item->getProduct

was not exactly returning the URL of the images, just giving me blank hence the default magento thumbnails. So I managed to get a workaround this, may not be the best way to do it but it works. For all the in cart product details I used:

$quote->getItemsCollection() as $item

This allowed me to get the product name, price and qty. But to get the image I had to get the Product ID and use that to get the Thumbnail. As all products in the store were configurable products I had to add 2 more lines to find the parent ID and then use that for the thumbnail. Normally you'd just set it on the admin end to use parent image, but wasn't working for this widget and I wasn't in the right mind to solve that out right now. So just entered the code to find the parent ID. Anyway code block is below just in case anybody comes across this issue and need something to build upon.

<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
    <div class="cart-content-image">
    <?php 
    $conid = $item->getProductId();
    $simpleProductId = $conid;
    $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($simpleProductId);
    $product = Mage::getModel('catalog/product')->load($parentIds[0]);
    ?>
        <img src="<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
    </div>
    <div class="cart-content-details">
        <?php 
            $maxLength = 50;
            $productName = $item->getName();
            echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
            echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
            echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
            echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
        ?>
    </div>
</div>
<?php }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.