cancel
Showing results for 
Search instead for 
Did you mean: 

Image Not Shown Unless Added & Deleted First

Image Not Shown Unless Added & Deleted First

I've been working on a module that adds Configurable Child Products to Cart at the Simple Product they are. This all works fine.

The part of the module I'm working on now is to get the Child Product Image. If there is no Child Product Image, then to use the Parent Configurable Image instead.

This does work. But it only works for Configurable Child Products once I have uploaded an image to them (any image) and then removed it. It then inherits the Configurable Product Image correctly.

If I create a Configurable Child Product and don't upload an image and then delete it first, it just takes the image from another product in the Cart

Please can anyone help me figure why it is working this way? Thank you.

 

<?php
namespace Configurable\Products\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Catalog\Helper\Image;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Catalog\Model\Product;

class Data extends AbstractHelper
{
/** @var \Magento\Framework\ObjectManagerInterface * */
    private $objectManager;
/** @param \Magento\Framework\ObjectManagerInterface $objectmanager * */
    protected $productModel;
    public function __construct(
        Context                                $context,
        Product                                $productModel,
        private readonly StoreManagerInterface $storeManager,
        private readonly Image                 $imageHelper,
        \Magento\Framework\ObjectManagerInterface $objectmanager
    )
    {
        $this->productModel = $productModel;
        $this->objectManager = $objectmanager;
        parent::__construct($context);
    }

    public function getImage($productId) {
      $product = $this->productModel->load($productId);  
      $productId = $product->getId();

      $productimage = $this->imageHelper->init($product, 'cart_page_product_thumbnail')->getUrl();

      $configurableProductResource = $this->objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable');

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

      // If Product is part of Configurable (Has Configurable Parent)
      if (isset($parentIds[0])) {
        $parentProductId = $parentIds[0];
        // Load the parent product
        $productRepository = $this->objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
        $parentProduct = $productRepository->getById($parentProductId);

        // Get the parent product's image
        $childimageUrl = $this->imageHelper->init($product, 'cart_page_product_thumbnail')->getUrl();
        $parentimageUrl = $this->imageHelper->init($parentProduct, 'cart_page_product_thumbnail')->getUrl();
            
        // if Child Image Exists, Show it
        if ($product->getImage() && $product->getImage() != 'no_selection') {
          $imageUrl =  $childimageUrl;
        }
        
        // Show Configurable Parent Image
        else {
          $imageUrl =  $parentimageUrl; 
        }
      }
      
      // Normal Product Image (not a Child Product of a Configurable Product)
      else {
        $imageUrl = $productimage;
      }

      return $imageUrl;
    }
}