Frage

if the number of products falls below 1, then it will become out of stock, but if you increase the products manually, then the status will remain out of stock. How can I automatically change the status to In Stock if the goods have been added manually?

I tried using the module from this answer, but it doesn't work.

My Magento v 2.3.2

War es hilfreich?

Lösung

I assume you know the necessary files for creating a module, one all basic files generated, create below files:

  1. Vendor/Module/etc/events.xml

     <?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 instance="Vendor\Module\Observer\Backend\Catalog\ProductSaveAfter" name="vendor_module_observer_backend_catalog_productsaveafter_catalog_product_save_after"/>
         </event>
     </config>
    
  2. Vendor/Module/Observer/Backend/Catalog/ProductSaveAfter.php

     <?php
    
     declare(strict_types=1);
    
     namespace Vendor\Module\Observer\Backend\Catalog;
    
     class ProductSaveAfter implements \Magento\Framework\Event\ObserverInterface
     {
         protected $product;
         protected $stockStateInterface;
         protected $stockRegistry;
    
         public function __construct(
             \Magento\Catalog\Model\Product $product,
             \Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
             \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
         ) {
             $this->product = $product;
             $this->stockStateInterface = $stockStateInterface;
             $this->stockRegistry = $stockRegistry;
         }
    
         public function execute(
             \Magento\Framework\Event\Observer $observer
         ) {
             try {
                 $productObserver = $observer->getProduct();
                 if($productObserver->getTypeId() != 'configurable') {
                     $productId = $productObserver->getId();
                     $stockItem = $this->stockRegistry->getStockItem($productId);
                     $productQty = $stockItem->getQty();
                     if($productQty > 0) {
                         $stockItem->setData('is_in_stock',1);       
                     } else {
                         $stockItem->setData('is_in_stock',0);
                     }
                     $stockItem->save();
                 }
             } catch (\Exception $e) {
                 return $e->getMessage();
             }
         }
     }
    

Run setup:upgrade and necessary commands depending on your mode and now change stock of the product and see, if stock is 0, it will automatically change stock status to 'Out of stock' and if stock is 1 or above, it will automatically change stock to 'In stock'

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top