Question

I have this afterGetList plugin in

Vendor\Module\Plugin\Magento\Sales\Model\OrderCompanyAccount.php

public function afterGetList(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        $orders
    )
    {
        foreach ($orders->getItems() as $order) {
        //do stuff
        }
        return $orders;
    }

And in Vendor\Module\etc\webapi_rest\di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="order_repository" type="Vendor\Module\Plugin\Magento\Sales\Model\OrderCompanyAccount"
                disabled="false"
                sortOrder="200"/>
    </type>
</config>

I want to add another afterGetList plugin.

Vendor\Module\Plugin\Magento\Sales\Model\OrderComment.php

public function afterGetList(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        $orders
    )
    {
        foreach ($orders->getItems() as $order) {
        //do other stuff
        }
        return $orders;
    }

And this time in Vendor\Module\etc\webapi_rest\di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Sales\Api\OrderComment">
        <plugin name="order_repository" type="Vendor\Module\Plugin\Magento\Sales\Model\OrderComment"
                disabled="false"
                sortOrder="210"/>
    </type>
</config>

If I enable one of these modules, they are working fine. However, if I enable both, none of them are working anymore. I tried with sortOrder but it has no effect.

So, my question is: How to have more than one afterGetList plugin enabled at the same time? Or better asked: What's the correct way of having two different plugins for one method at all?

Was it helpful?

Solution

I think this should work, but you added the same plugin name on both instances plugin name="order_repository" and according to the doc, we have:

<plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false" />
-> plugin name - an arbitrary plugin name that identifies a plugin. Also used to merge the configurations for the plugin.

So basically if you enable both your plugins, then the configuration will be merged based on the plugin name which is used as identifier and only one of them will work.

Just add a unique identifier for each of your plugins and try again.

Good Luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top