Magento 2 checkout cart page is the intermediate point that proceeds to finalize the purchase. The product image displayed on the checkout cart page gives the customer a quick overview of the items they are about to purchase.
However, when the product has multiple colors and options the image might not match the desired custom selection of the customer.
Moreover, when a store owner wants to give a free product based on a condition, and that product belongs to the customer’s selected configured one.
In that case, what if a store owner needs to display an image of the main product in the free one?
Any solution? Here is the quick solution. Apply the below method to change product image in Magento 2 checkout cart!
Method to Change Product Image in Magento 2 Checkout Cart
1. Use the below code in the di.xml file at app/code/Vendor/Extension/etc
<type name="Magento\Checkout\Block\Cart\Item\Renderer"> <plugin name="product_image_checkout_cart" type="Vendore\Extension\Plugin\CheckoutCart\NewImage" sortOrder="1"/> </type>
2. Create the NewImage.php file at Vendor/Extension/Plugin/CheckoutCart
<?php namespace Vendore\Extension\Plugin\CheckoutCart; class NewImage { public function afterGetImage($item, $result) { if(CONDITION) { $result->setImageUrl(IMAGE_URL); } return $result; } }
That’s it!
Feel free to share the solution with Magento Community via social media.
Thank You.