We have a need to sometimes fetch product prices from an API for products loaded on the frontend. We figured it would be easiest to create a plugin for the collection:
<?xml version="1.0"?>
<!-- .../Vendor/Module/etc/frontend/di.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
...
<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
<plugin name="afterProductCollectionPlugin" type="Vendor\Module\Plugin\Catalog\Model\ResourceModel\Product\CollectionPlugin" />
</type>
</config>
<?php
namespace Vendor\Module\Plugin\Catalog\Model\ResourceModel\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
class CollectionPlugin
{
/**
* @param Collection $subject
* @param bool $printQuery
* @param bool $logQuery
* @return array
*/
public function afterLoad(Collection $subject, $printQuery = false, $logQuery = false)
{
return [$printQuery, $logQuery];
}
}
However after flushing the caches the /checkout/cart
page throws a fatal error:
<b>Fatal error</b>: Uncaught Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection\Interceptor::isComposite() in C:\xampp\htdocs\vendor\magento\module-catalog\view\frontend\templates\product\list\items.phtml:180
Stack trace:
#0 C:\xampp\htdocs\vendor\magento\framework\View\TemplateEngine\Php.php(59): include()
#1 C:\xampp\htdocs\vendor\magento\framework\View\Element\Template.php(271): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Checkout\Block\Cart\Crosssell), 'C:/xampp/htdocs...', Array)
#2 C:\xampp\htdocs\vendor\magento\framework\View\Element\Template.php(301): Magento\Framework\View\Element\Template->fetchView('C:/xampp/htdocs...')
#3 C:\xampp\htdocs\vendor\magento\framework\View\Element\AbstractBlock.php(1097): Magento\Framework\View\Element\Template->_toHtml()
#4 C:\xampp\htdocs\vendor\magento\framework\View\Element\AbstractBlock.php(1101): Magento\Framework\View\Element\AbstractBlock->Magento\Framework\View\Element\{closure}()
#5 C:\xampp\htdocs\vendor\magen in <b>C:\xampp\htdocs\vendor\magento\module-catalog\view\frontend\templates\product\list\items.phtml</b> on line <b>180</b><br />
How to fix this?