Solved: No Image for Configurable Product in Magento 2 Cart Page
Magento 2 store owners have many options available to optimize the store functionality, improve customer experience, capture payments and ease the administrative tasks with 3rd party extensions. Also, there are 3rd party custom themes available to improve the look and feel of the admin panel and storefront.
The only problem with these 3rd party themes and extensions is that sometimes it disrupts the features of default Magento 2 or conflicts with default configuration.
One such issue is No Image for Configurable Product in Magento 2 Cart Page. The simple product image of a configurable product is not displayed in the cart page due to 3rd party extensions or themes.
Generally, the developers follow below steps for fixing the problem:
Preconditions
- Magento 2.2.x
- Create a configurable product.
Steps to Reproduce
- Create a configurable product.
- Add images for all child products
- Do not add images for parent products.
- Add that product in cart page.
It is expected that images of child product are displayed on cart page. If a child product has no images then images of the parent product are displayed.
Unfortunately, it’s not the case ?
To overcome this issue, one needs to override the Magento 2 code file. Let’s see how to do it.
Solution: No Image for Configurable Product in Magento 2 Cart Page
Copy [MAGENTO_ROOT]vendormagentomodule-catalogBlockProductImageBuilder.php and paste it to [MAGENTO_ROOT]appcodeMagentoCatalogBlockProductImageBuilder.php
find the following code:
1 2 3 4 5 6 |
if ($simpleOption !== null) { $optionProduct = $simpleOption->getProduct(); $this->setProduct($optionProduct); } |
And, replace it with the below code:
1 2 3 4 5 6 7 8 9 |
if ($simpleOption !== null) { $optionProduct = $simpleOption->getProduct(); $objectManager = MagentoFrameworkAppObjectManager::getInstance(); $product = $objectManager->create('MagentoConfigurableProductModelResourceModelProductTypeConfigurable')->getParentIdsByChild($optionProduct->getEntityId()); $parentProduct = $objectManager->create('MagentoCatalogModelProduct')->load($product[0]); $this->setProduct($parentProduct); } |
That’s all you have to do to solve No Image for Configurable Product in Magento 2 Cart Page!
The post will be helpful to display the product images of the configurable product in Magento 2 cart page. If not, please mention your doubts in the comments section and I’d be happy to solve the issue ?
Feel free to rate the solution with 5 stars ?
Thank You ?
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Hi. Magento say to not use the object manager directly. How would you achieve this using block and dependancy method?
Hello Jon,
Please refer https://meetanshi.com/blog/create-plugin-interceptor-in-magento-2/ for the alternate method.
Thanks.