0

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?

2
  • Why do you want two plugin for same method ? Mar 18, 2021 at 18:52
  • One#s doing one thing, the other is doing something else. I thought it would be better to have two plugins instead of writing both into one. I that wrong?
    – zlep
    Mar 19, 2021 at 17:18

1 Answer 1

1

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!

2
  • 1
    Yes, that's it. Sometimes, mistakes are so stupid. It's working now. Thanks.
    – zlep
    Mar 16, 2021 at 10:08
  • no poblem :D sometimes fresh eyes do the trick :D
    – Diana
    Mar 16, 2021 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.