Reputation: 123
I have been trying to work this out for the past hour. 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 grad 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, 'image')->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, 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName();?>" />
I have searched for the past hour, 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
Modified Code
<?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(), 'image')->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 }
?>
Upvotes: 2
Views: 2685
Reputation: 123
Ok finally got this to work. Did more look around on the net and thanks to @Robbie Averill 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 }
?>
Upvotes: 0
Reputation: 24406
Try ...->init($item->getProduct(), 'image')...
as the product is a component of the cart item ($item
) - see Mage_Sales_Model_Order_Item::getProduct()
.
The Mage_Catalog_Helper_Image::init()
method expects an instance of Mage_Catalog_Model_Product
, but you'd be passing in the cart item which is a Mage_Sales_Model_Order_Item
(or Mage_Sales_Model_Quote_Item
):
# File: app/code/core/Mage/Catalog/Helper/Image.php
/**
* Initialize Helper to work with Image
*
* @param Mage_Catalog_Model_Product $product
* @param string $attributeName
* @param mixed $imageFile
* @return Mage_Catalog_Helper_Image
*/
public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
{
// ...
}
Adjusted code should work:
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(), 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
...
Upvotes: 1