0

I would like to fetch related products while select configurable product drop downs hence i am using plugin concept.

I am unable to fetch the related products for the simple product. it is throwing the product already existed in the frontend. Can any one advise me please what i is wrong here.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable">
        <plugin disabled="false" name="configurable" sortOrder="10" type="Demo\ConfigurableSku\Plugin\Magento\ConfigurableProduct\Block\Product\View\Type\Configurable"/>
    </type>
</config>

app/code/Demo/ConfigurableSku/Plugin/Magento/ConfigurableProduct/Block/Product/View/Type/Configurable.php

<?php

namespace Demo\ConfigurableSku\Plugin\Magento\ConfigurableProduct\Block\Product\View\Type;
use Magento\Framework\Registry;
use Psr\Log\LoggerInterface;
class Configurable
{
    /**
     * @var Registry
     */
    protected $registry;

     /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $product;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * Layout
     *
     * @var \Magento\Framework\View\LayoutInterface
     */
     protected $layout;

     public function __construct(
        Registry $registry,
        \Magento\Catalog\Model\ProductFactory $product,
        LoggerInterface $logger,
        \Magento\Framework\View\LayoutInterface $layout
    ) {
        $this->registry = $registry;
        $this->product = $product;
        $this->logger = $logger;
        $this->layout = $layout;
    }

    public function afterGetJsonConfig(\Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject, $result)
    {
        $jsonResult = json_decode($result, true);  
        $jsonResult['related']= [];
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $productFactory = $objectManager->get('Magento\Catalog\Model\ProductFactory');     
        foreach ($subject->getAllowProducts() as $simpleProduct) 
        {
            $jsonResult['related'][$simpleProduct->getId()]= $this->getRelated($simpleProduct->getId());      
        }      
        $result = json_encode($jsonResult);     
        return $result;
    } 

     public function getRelated($productId) {
        try{            
            $product = $this->getProduct($productId);
            $this->registry->register('product', $product);
            $relateProducts = $product->getRelatedProducts();       
                if($relateProducts) {
                    $block = $this->layout->createBlock("Magento\Catalog\Block\Product\ProductList\Related")
                    ->setTemplate("WeltPixel_OwlCarouselSlider::product/list/items.phtml")                
                    ->setData('type', 'related');                
                    return $block->toHtml();
                } else {
                    return '';
                }       
            return '';
        }catch(\exception $e){
            
        }
    }

    public function getProduct($productId) {
        $product = $this->product->create()->load($productId);
        if ($product) {
            return $product;
        }
        return '';
    }

}
  • Related products is already available in blocks and you can adjust it's location on the page . What you are trying to achieve here? – Arif Ahmad 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.