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.
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.