3

I want to be able to observe any product's stock changing in any way, without having to do so with database triggers.

In Magento 1.x the lowest level I could find that does this consistently is the Stock Item's resource model correctItemsQty method that actually writes product quantities to the database.

In Magento 2.x, I've tried to create a plugin for \Magento\CatalogInventory\Model\ResourceModel\Stock::correctItemsQty in the form of MyPlugin::afterCorrectItemsQty. The configuration for the plugin is pretty standard:

<type name="Magento\CatalogInventory\Model\ResourceModel\Stock">
    <plugin name="my_stock_item_plugin"
        type="My\Company\Plugin\StockPlugin"
        sortOrder="100" />
</type>

My method just for testing just has a die('plugged in') in it.

After setup:di:compile and clearing cache, this doesn't run when I place an order in the frontend or admin, ship an order, credit memo an order etc. (In Magento 1.x this method does fire).

To eliminate my plugin being the problem I added my die call into the top of the core method in the core magento-catalog-inventory module, but it's still not executing.

Am I missing something here? Has there been a behavioural change that means that this method doesn't get called when you place an order (for example)?

Aside from that, is there a better way that I can observe all product stock changes in Magento 2?

Disclaimer: I've seen this question and this core bug. This code is for a module, so I don't have the liberty to wait for the core fix to be released, or monkey patch it in. I've also checked from my docker container that my changes are syncing through (they are) to ensure it's not a local filesync problem.

  • can you please write whole di.xml code? – Sunil Patel May 25 '17 at 10:33
  • @SunilPatel it's not really about the plugin here, since a manual addition to the core model doesn't get executed either. I think it's an architectural issue I'm having here, and looking for advice about which direction I should go down. – Robbie Averill May 25 '17 at 10:40
3
+300

I dont know why your plugin does not work, maybe there is some kind of misprint? I reproduce all steps on my 2.1.3 and all works fine:

In the global di.xml:

<type name="Magento\CatalogInventory\Model\ResourceModel\Stock">
    <plugin name="my_stock_item_plugin"
            type="Vendor\Module\Plugin\StockPlugin"
            sortOrder="100" />
</type>

Plugin class:

<?php

namespace Vendor\Module\Plugin;

class StockPlugin
{

    public function afterCorrectItemsQty($subject, $result)
    {
        $a = 0;

        return $result;
    }
}

Result in debug:

result

Anyway I'll recommend you to use the "around" plugin where you can check qty change:

<?php

namespace Vendor\Module\Plugin;

class StockPlugin
{

    public function aroundCorrectItemsQty($subject, \Closure $proceed, array $items, $websiteId, $operator)
    {
        $proceed($items, $websiteId, $operator);

        // Do something with $items, $websiteId, $operator here
    }
}

Result of around plugin where you can make changes before commit and after:

around plugin

All works fine in the admin area too:

Order placed

Result in debug:

result

PS: I think use of die() for debug purpose is not a good idea, I'll recommend to use the xDebug or log instead.

  • Thanks. I was afraid of this being something environmental, but you're probably right! – Robbie Averill May 29 '17 at 10:24
  • @RobbieAverill When i face such an issue and see that everything should work but it doesn’t, then i just delete everything ( but save it ) and rewrite everything anew – Siarhey Uchukhlebau May 29 '17 at 10:33
  • Yep, turns out there was something wrong with my local M2 docker dev box. It's working fine after trying in a different environment. Thanks for your answer though, helpful! – Robbie Averill May 29 '17 at 19:53

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.