Event-Observer for whenever the stock status of product changes
文章目录
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
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论