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('Magento\Catalog\Helper\Image');
$productImage = $_imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();

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

$product = $_product->getId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product);
     //this is parent product id..
     //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
  • Hi, Ok, that works, but I don't think it's good to use ObjectManager in a template file? – ineluki Sep 11 '17 at 11:26

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.