0

I am trying to change product image in minicart,

Used below code to achieve it.

here is the code of my di.xml file.

<type name="Magento\Checkout\CustomerData\AbstractItem">
 <plugin name="Change_Product_Image_In_Minicart" type="Vendor\Module\Plugin\Minicart\Image" sortOrder="1"/>
</type>

This code i used in my plugin file Image.php

 class Image
 {
    public function aroundGetItemData($subject, $proceed, $item){
     $result = $proceed($item);
     $productImage = "some image link"; 
     $result['product_image']['src'] = $productImage;   
     return $result;
   }
 }

I am getting below erro once used above code,

PHP Parse error: syntax error, unexpected '\xc2\xa0\xc2\xa0public' (T_STRING), expecting function (T_FUNCTION), Anyone suggest what is mistake here please, }

I have fixed the issue myself, can anybody update me code for, how to change image for each item in the minicart, suppose if i have 5 items in cart, i need to change image for each of them, How can i apply for each here?

7
  • which type of image you want , is that custom image attribute or one of product gallery image ? Oct 4, 2018 at 7:21
  • Some image url is stored in custom table, where i am wrong here? Oct 4, 2018 at 7:22
  • did you directly copy paste the code from the browser, a PDF, or an advanced text editor, I think that error because the encoding problem ?
    – mrfizh
    Oct 4, 2018 at 7:23
  • did you forget class closing { ? Oct 4, 2018 at 7:27
  • no, i am closing it Oct 4, 2018 at 7:36

1 Answer 1

0

In this topic we will do How to Change Product Image in Minicart

Step 1: Create di.xml file

Step 2: Create Image.php file

Product image in minicart will getten to show by function doGetItemData() in Magento\Checkout\CustomerData\DefaultItem class but this function is protected, so we cannot use plugin on it. Instead, it extends from Magento\Checkout\CustomerData\AbstractItem and use function getItemData()

First We need to create registration and module file as per structure of magento 2 then follow below steps.

Step 1: Create di.xml file

Create di.xml file in app/code/[Name_Space]/[Your_Module]/etc/frontend

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\CustomerData\AbstractItem">
     <plugin name="Change_Product_Image_In_Minicart" type="[Name_Space]\[Your_Module]\Plugin\Minicart\Image" sortOrder="1"/>
</type>
</config>

Step 2: Create Image.php file

Create Image.php file in app/code/[Name_Space][Your_Module]\Plugin\Minicart

<?php

namespace [Name_Space]\[Your_Module]\Plugin\Minicart;

class Image
{

    public function aroundGetItemData($subject, $proceed, $item)
    {

        $result = $proceed($item);

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($result['product_id']);

        /* thum url */ 
        $storeManager = $objectManager->create('Magento\Store\Model\StoreManagerInterface'); 
        $currentStore = $storeManager->getStore();
        $mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

        if($product->getThumbnail()){
            $result['product_image']['src'] = $mediaUrl.$product->getThumbnail();
        }
        else{
            $result['product_image']['src'];
        }
        return $result;
    }
}

Your Answer

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

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