0

I am trying to change the cart image based on my cart item configuration. But it seems like Magento\Checkout\Block\Cart\Item\Renderer::getImage receives the original Product model class and not the cart item product.

When adding the item to the cart I have added custom data with $product->addCustomOption('my_option', $data) and in $data is a generated base64 encoded image of the product. Which I would like to display in the cart and minicart.

Any ideas on how to solve this?

1

4 Answers 4

1

I don't understood well the question, but if you want to change which product's image of configurable product will be showed in cart and minicart, in latest Magento version you can do it in: Store-> Configuration-> Sales-> Checkout-> Shopping Cart-> Configurable Product Image Where you can choose between: Product Thumbnail Itself or Parent Product Thumbnail

0

I don't think you would able to changes the cart image by changing anything at Magento\Checkout\Block\Cart\Item\Renderer::getImage.

Best idea in my view that work on template file means working on .phtml.

If you have your custom option 'my_option' then show our custom option image else show image using <?= $block->getImage($block->getProductForThumbnail(), 'cart_page_product_thumbnail')->toHtml() ?>

First, you need to find out cart your items render phtml file, you don't have any customization then cart/item/default.phtml is the file from where the images are showing at cart page.

1
  • Thank you for your answer, I will have to test it when I get back to the office.
    – Tuim
    Jun 4, 2018 at 11:44
0

I don´t know if I have undestood you correctly, but this is what I have done to show images on the minicart:

  1. Go to: /app/design/frontend/Venustheme/mohawk/Magento_Checkout/templates/cart/minicart.phtml

This is my code:

<?php if ($grandTotal < 0.01): ?>

<img src="/pub/media/wysiwyg/image_folder/1.png" />

<?php endif ?>

<?php if ($grandTotal > 0.01 AND $grandTotal < 120): ?>

<img src="/pub/media/wysiwyg/image_folder/2.png" />

<?php endif ?>

Depending the minicart subtotal price, it shows a image or another.

Oh, and you must declare de variable "subtotal", that is:

$subTotal = $cart->getQuote()->getSubtotal();

RESULT:

enter image description here

1
  • Thank you for your answer, I will have to test it when I get back to the office.
    – Tuim
    Jun 4, 2018 at 11:44
-1

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;
    }
}
1
  • Don't use object manager, a full product load is bad practice.
    – omsta
    Jun 14, 2022 at 9:06

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.