TransWikia.com

Magento2 Get parent image from a simple product (configurable)

Magento Asked by ineluki on February 9, 2021

How can I get parent image from a simple product if the simple product has no base image?

The following code gets the simple product image in a template file.

$_imagehelper = $this->helper('MagentoCatalogHelperImage');
$productImage = $_imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();

One Answer

Assuming you want the parent of the simple product you would use the following

$product = $_product->getId();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product = $objectManager->create('MagentoConfigurableProductModelResourceModelProductTypeConfigurable')->getParentIdsByChild($product);
 if(isset($product[0])){
     //this is parent product id..
     $product[0];
     //here you can load up the parent product and get its image just rememeber to wrap in an if statement to check if child product image is set
}

Answered by Dava Gordon on February 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP