문제

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?

도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top