How to change product image in mini cart Magento 2

Here in this article, we have given two steps that shows you how to change Product Image in mini cart Magento2

Step 1: create a di.xml file

Step 2: create an image.php file

Product image in mini cart will attain to show the function doGetItemData() in Magento\Checkout\CustomerData\DefaultItem class but this function is protected,so we cannot use plugin in it. Instead, it expand from Magento\Checkout\CustomerData\AbstractItem and use function getItemData()

  • You can use plugin in the following code:
/**

* {@inheritdoc}

*/

public function getItemData(Item $item)

{

     $this->item = $item;

     return \array_merge(

         ['product_type' => $item->getProductType()],

         $this->doGetItemData()

     );

}

Create di.xml file

To create a di.xml file use

 app/code/[Name Space]/[Your Module]/ete=c/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>

create image.php file

To create an image.php file use

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);

     if(CHECK_IF_YOU_NEED_TO_REPLACE_IMAGE) {

     $result['product_image']['src'] = YOUR_NEW_IMAGE;

     }

     return $result;

    }

}

The above given steps will help you to change mini cart product image in Magento 2.

Last Update: February 28, 2019  

February 5, 2018   640   Nandini R    Products    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn