关于magento2:EventObserver-for-whenever-the-stock-status-of-product-changes

7次阅读

共计 1054 个字符,预计需要花费 3 分钟才能阅读完成。

You can use catalog_product_save_after, checkout_submit_all_after ,cataloginventory_stock_revert_products_sale, sales_order_item_cancel(Depending) and get the stock status. You can even check for that product attribute value is as desired out of stock and that out of stock do nothing else update the attribute value .
Put this events.xml in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And put your Productsaveafter.php in below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{public function execute(\Magento\Framework\Event\Observer $observer)
    {$_product = $observer->getProduct(); // you will get product object and you can check for stock and attribute values here

    }   
}
正文完
 0