2

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-&gt;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-&gt;fetchView('C:/xampp/htdocs...')
#3 C:\xampp\htdocs\vendor\magento\framework\View\Element\AbstractBlock.php(1097): Magento\Framework\View\Element\Template-&gt;_toHtml()
#4 C:\xampp\htdocs\vendor\magento\framework\View\Element\AbstractBlock.php(1101): Magento\Framework\View\Element\AbstractBlock-&gt;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?

3

In case you are using the after plugin, you must have a $result in the arguments first, and it must return the $result. See example:

<?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, $result, $printQuery = false, $logQuery = false)
    {
        return $result;
    }
}

PS: more info in DevDocs

| improve this answer | |

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.