1

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

  • 1
    You might need to contact the module developer to get help with this functionality. – rob3000 Aug 10 '15 at 6:01
2
+50
<?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'; 
  }
} 
?>
  • Thanks @HarryP. Can you tell me the changing of name also. That will be great. – Kishor Aug 14 '15 at 11:26
  • $message = $this->__('%s was successfully added to your shopping cart.', $simpleProduct->getName()); Place above code under this code $simpleProduct=Mage::getModel('catalog/product')->loadByAttribute('sku', $product->getSku()); – Yogesh Trivedi Aug 14 '15 at 11:30
  • Sorry. that worked fine. I was doing mistake. – Kishor Aug 15 '15 at 13:30
1

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.

1
    <?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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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