Magento – Magento 2 Plugin – ProductRepository getList override

dependency-injectiondimagento2plugin

I'm a beginner with Magento 2 and am having some issues with plugins – I've successfully done a plugin to change a simple object get statement (after event) – and in this case after compiling my code I've seen the generated files inside the var/generation folder.

However, when trying to tap in to the ProductRepository getList method, I'm having some issues – it's not doing anything.

Is there a log I can check after compilation to try to find out what the issue is?

I've tried before, around, after – different settings, but can't get it to work. Am I missing something?

I just need to add final price to the product collection. The example below obviously won't do this, but it doesn't even get compiled.

Can someone hint as to how to achieve this? Perhaps by using 'aroundGetList' – and show me which parameters would work?

Plugin class:

<?php

    namespace MyCo\MyExt\Plugin;


    class ProductOverride {

        public function afterGetList($subject) {

            $subject->addFinalPrice();


        }

di.xml file

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Model\ProductRepository">
        <plugin name="MyListener" type="MyCo\MyExt\Plugin\ProductOverride" sortOrder="1" />
    </type>

</config>

The method I want to plug in to has this signature:

 /**
     * Get product list
     *
     * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
     * @return \Magento\Catalog\Api\Data\ProductSearchResultsInterface
     */
    public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
}

The method already appears to have a generated interceptor file also… is it wise to do it this way if i just want to call $collection->addFinalPrice();?

   public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    {
        $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getList');
        if (!$pluginInfo) {
            return parent::getList($searchCriteria);
        } else {
            return $this->___callPlugins('getList', func_get_args(), $pluginInfo);
        }
    }

Best Answer

My guess after looking at the native plugins is that you should create your plugin on Magento\Catalog\Api\ProductRepositoryInterface instead.

If you check the native plugins linked to Repository classes, almost all of them are not linked to the repository class directly but to the preference interface.

Quick example:

<type name="Magento\Customer\Api\GroupRepositoryInterface">
    <plugin name="invalidatePriceIndexerOnCustomerGroup" type="Magento\Catalog\Model\Indexer\Product\Price\Plugin\CustomerGroup"/>
</type>

This plugin is linked to the repository interface and the preference for this class is the repository class itself:

<preference for="Magento\Customer\Api\GroupRepositoryInterface"
            type="Magento\Customer\Model\ResourceModel\GroupRepository" />

I'm not sure if this is a restriction regarding plugins but my suggestion would be to change your code to this and try again:

<type name="Magento\Catalog\Api\ProductRepositoryInterface">
    <plugin name="MyListener" type="MyCo\MyExt\Plugin\ProductOverride" sortOrder="1" />
</type>