0

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();

2 Answers 2

1

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

$product = $_product->getId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configurableProductResource = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable');

// Get the parent product IDs for the given child product
$parentIds = $configurableProductResource->getParentIdsByChild($product);

if (isset($parentIds[0])) {
    $parentProductId = $parentIds[0];
    
    // Load the parent product
    $productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
    $parentProduct = $productRepository->getById($parentProductId);

    // Check if the child product has an image set
    if ($_product->getImage() !== 'no_selection') {
        // Get the parent product's image
        $parentProductImage = $parentProduct->getImage();
        
        // You can now use $parentProductImage as the parent product's image
        echo "Parent Product Image: " . $parentProductImage;
    }
}

This code does the following:

  1. Gets the parent product IDs for the given child product using $configurableProductResource->getParentIdsByChild($product).
  2. Checks if there is a parent product ID in the $parentIds array.
  3. If a parent product ID is found, it loads the parent product using the ProductRepositoryInterface.
  4. It checks if the child product has an image set by comparing it to 'no_selection'
  5. If the child product has an image set, it retrieves the parent product's image and stores it in the $parentProductImage variable, which you can use as needed.

Please make sure that you're using the Magento service contracts (like ProductRepositoryInterface) whenever possible instead of directly using the object manager for better code practices and compatibility with future versions of Magento.

6
  • 1
    Hi, Ok, that works, but I don't think it's good to use ObjectManager in a template file?
    – ineluki
    Commented Sep 11, 2017 at 11:26
  • This is helpful but it would be more useful if it was complete and retrieved the image. Can anyone complete this? Commented Oct 27, 2023 at 12:09
  • @ChrisOliver have modified my answer to give you a more accurate answer. Commented Nov 2, 2023 at 17:17
  • Thank you @DavaGordon, that looks good. The way I am wanting to use this is if the child product has no image, then inherit the configurable parent image, if the child product does have an image then use that. So the preference is for a simple product to use it's own image but if there isn't one, to use the parents. Removing ! from this would achieve that? if ($_product->getImage() !== 'no_selection') { Commented Nov 2, 2023 at 20:37
  • @ChrisOliver at this point $product = $_product->getId(); you want to check if the simple product has an image so something like if($_product->getImage()) { //return simple image }else { the code from my comment. } Commented Nov 3, 2023 at 15:29
0

I built on the previous answer to get the image url, should this help anyone.

// Show Configurable Parent Image
$product = $product->getId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product);
 if(isset($product[0])){
  $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product);
  $imageBlock =  $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
  $productImage = $imageBlock->getImage($product, 'cart_page_product_thumbnail');
  $producturl = $product->getProductUrl();
  $productimagehtml = $productImage->toHtml();
    $thumbnail = $productimagehtml;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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