How to change product image in mini cart magento 2

The two steps shows you how to change product image in mini cart magento2

Step 1: create di.xml file

Step 2: create image.php file

Product image in mini cart will obtain 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()

  • 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 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 a 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 mentioned steps help you to change product image mini cart in magento 2

Last Update: April 9, 2018  

February 5, 2018   328   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
Google+
http://www.webnexs.com/blog/kb/change-product-image-mini-cart-magento-2
Twitter
LinkedIn