John Davidson

php - Update IsInStock attribute in observer by 'catalog_product_save_after' event in Magento2.3


I have tried this code in Magento 2.3.4

  1. created events.xml in Dapl\Shortdurability\etc\adminhtml

2.created Productsaveafter.php in Dapl\Shortdurability\Observer.

namespace Dapl\Shortdurability\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\ResourceModel\Product\Action;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class Productsaveafter implements ObserverInterface
private $action;
private $stockItemRepository;

public function __construct(
StockItemRepository $stockItemRepository,
Action $action
$this->action = $action;
$this->stockItemRepository = $stockItemRepository;

public function execute(\Magento\Framework\Event\Observer $observer)
$_product = $observer->getProduct();
$_id = $_product->getId(); // for Id
$_shortdurability = $_product->getShortdurability();
$_visibility = $_product->getVisibility();
$_qty = $this->stockItemRepository->get($_id);
$quantity = $_qty->getQty();
if(($_shortdurability == 1) && ($quantity == 0))
$this->action->updateAttributes([$_id], ['visibility' => 1], 0);

} else {
$this->action->updateAttributes([$_id], ['visibility' => 4], 0);
//$this->action->updateAttributes([$_id], ['quantity_and_stock_status' => ['is_in_stock' => 1]], 1);

Hi. I can't use save method to set IsInStock attribute value. Because, it's going to infinite loop. finaly i have tried every set method. But stock status has not changed.
i have also tried updateStockItemBySku($sku, $stockItem), updateAttributes([$_id], ['quantity_and_stock_status' => ['is_in_stock' => 1]], 1).
please, help me. I have searched about it from google, still i am not able to update IsInStock attribute of product, in Magento 2.3.4.

My Answer:
creating a custom observer in Magento 2.

First, create a new module or use an existing one. Then, create a new PHP file for your observer class.

In this file, you can define your observer class and method to update the IsInStock attribute. Here is an example code snippet:


namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\ProductFactory;

class UpdateIsInStock implements ObserverInterface
protected $productFactory;

public function __construct(
ProductFactory $productFactory
) {
$this->productFactory = $productFactory;

public function execute(\Magento\Framework\Event\Observer $observer)
$product = $observer->getEvent()->getProduct();
$productId = $product->getId();

// Update IsInStock attribute
$productModel = $this->productFactory->create()->load($productId);
$productModel->setIsInStock(1); // Set IsInStock to true

Don't forget to add the necessary configuration in your module's `etc/events.xml` file to link the observer to the event you want to trigger it.

After creating the observer class and configuring it properly, the `execute` method will be called whenever the specified event occurs, and it will update the IsInStock attribute of the product accordingly.

Rate this post

3 of 5 based on 3391 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us