0

I used around plugin to call custom phtml in category list page. It's working proper. But, In configurable product it's hide to display swatches.

I upload here my files code. Which function should I need to use in plugin to working perfectly ?

di.xml :

<?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\Catalog\Block\Product\ListProduct">
        <plugin name="custom-module-category" type="Vendor\Module\Plugin\File" sortOrder="16"/>
    </type>
    <virtualType name="Magento\CatalogSearch\Block\SearchResult\ListProduct">
        <plugin name="custom-module-search" type="Vendor\Module\Plugin\File" sortOrder="17"  />
    </virtualType>
</config>

File.php :

public function aroundGetProductDetailsHtml(
    \Magento\Catalog\Block\Product\ListProduct $subject,
    \Closure $proceed,
    \Magento\Catalog\Model\Product $product
    ) {
    $data = $this->_productDocFactory->create()->load($product->getId(), 'product_id');
    if(count($data->getData())){
    return $this->layout->createBlock('Vendor\Module\Block\File')->setProduct($data_id)->setTemplate('Vendor_Module::file.phtml')->toHtml();
    } else {
    return '';
    }
    }
  • Where you have defined and set value for $data_id? – Rakesh Varma Jan 9 at 12:10
1

You didn't anything in else condition. So, it was display blank. Use this below code :

 public function aroundGetProductDetailsHtml(
    \Magento\Catalog\Block\Product\ListProduct $subject,
    \Closure $proceed,
    \Magento\Catalog\Model\Product $product
) {
    $data = $this->_producyFactory->create()->load($product->getId(), 'product_id');
    if (count($data->getData())) {
        return $this->layout->createBlock('Vendor\Module\Block\File')->setProduct($data)->setTemplate('Vendor_Module::file.phtml')->toHtml();
    } else {
        $result = $proceed($product);
        return $result;
    }
}

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.