Here in this article, we have given two steps that show you how to change Product Image in mini cart Magento 2.
Step 1: create a di.xml file
Step 2: create an image.php file
Interested in building an eCommerce marketplace like Amazon with Magento 2? Click here!!!
How to change product image in mini cart Magento 2?
The product image in the mini cart will attain to show the function doGetItemData() in Magento\Checkout\CustomerData\DefaultItem class, but this function is protected, so we cannot use the plugin in it. Instead, it expands from Magento\Checkout\CustomerData\AbstractItem and use function getItemData()
- You can use the 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 an image.php file in the Magento backend
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 the mini cart product image in Magento 2.
Get build your online store on the Magento platform with just a few steps!!!
Leave a Reply