Question

I have a problem of changing the image of configurable product in ajax cart based on product image. I want to have product image in ajax cart after selection.

Here is the controller code which should reflects the change.

if (!$this->_getSession()->getNoCartRedirect(true)) {
if (!$cart->getQuote()->getHasError()){
 $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
$img = "<img src='".Mage::helper('catalog/image')->init($product, 'image')->resize(60,null)."' />";
$tmp_product = '<div id="message_ajax"><div class="ajaxcart_image">'.$img.'</div><div class="ajaxcart_message">'.$message.'</div></div>';
$check = 'success'; 

enter image description here

Was it helpful?

Solution

<?php if (!$this->_getSession()->getNoCartRedirect(true)) {
   if (!$cart->getQuote()->getHasError()){
    $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
    $simpleProduct=Mage::getModel('catalog/product')->loadByAttribute('sku', $product->getSku());
    $img = "<img src='".Mage::helper('catalog/image')->init($simpleProduct, 'image')->resize(60,null)."' />";
    $tmp_product = '<div id="message_ajax"><div class="ajaxcart_image">'.$img.'</div><div class="ajaxcart_message">'.$message.'</div></div>';
    $check = 'success'; 
  }
} 
?>

OTHER TIPS

You'll have to loop through all products in the cart and search for a simple product with a relation to the configurable product. Then get the image from the simple product, for example:

<?php
foreach($cart->getQuote()->getAllItems() as $item){
    if($item->getParentItemId() == $product->getId()){
        $imageUrl = Mage::helper('catalog/image')->init($item, 'image')->resize(60,null);
    }
}
?>
<img src="<?=$imageUrl;?>">

I've not tested this code and if your product does have multiple options (like a color and a size) it may not work correctly. Then you'll have to create a check for that specific product option.

    <?php
$img = '';
        $mainProductId = $product->getId();
        foreach($cart->getQuote()->getAllVisibleItems() as $_item){
            if($mainProductId == $_item->getProductId()){
                 $ActualProduct=Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku());
                $img = Mage::helper('catalog/image')->init($ActualProduct, 'image')->resize(60,null);
            }

        }
// here you put the logic like if ($img != ''){show the image} etc
    ?>

Note that the $_item->getProductId() will return configurable product id while the $_item->getSku() will be of the simple product associated. I hope this will resolve your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top