1

I want to show base image in minicart and checkout, Right now it is showing thumbnail images in magento2.1.7

I don't have any thumbnail images for my product so I need to point it as base image.

Any help would be appreciated.

2

To Set Base Image on Minicart in Magento2, you just have to add a etc/view.xml into your own theme (f.e. app/design/frontend/Package/yourtheme/etc/view.xml) ans add something like that:

<image id="mini_cart_product_thumbnail" type="thumbnail">
    <width>72</width>
    <height>72</height>
</image>

To

<image id="mini_cart_product_thumbnail" type="image">
    <width>72</width>
    <height>72</height>
</image>
0
2

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
  • ahhhh you saved my day thanks man upvoted – Asad Ullah Sep 9 '20 at 9:18
0

If you have only 1 image in your product you need to set it as base, small and thumbnail.

1. go to product edit page enter image description here

2. click on it and set this values enter image description here

3. save product

3
  • There are many products, So i need to go and edit each and every product, It is very time consuming. – Amy Feb 12 '19 at 8:10
  • Is is possible to change in code and point it. – Amy Feb 12 '19 at 8:11
  • You can try this foreach ($products as $product){ $thumbnail = $product->getThumbnail(); $imagePath = '/catalog/product' . $product->getImage(); if( $thumbnail == 'no_selection' ){ echo 'FIXING ' . $imagePath . "\r\n"; $product->addImageToMediaGallery( $imagePath, array( 'thumbnail' ), false, false ); $product->save(); } } – Serhii Havrylchuk Feb 12 '19 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.