Magento: Auto-changing the “Stock Availability” from “Out of Stock” to “In Stock” (& vice-versa) on Quantity Change

前端 未结 5 1575
刺人心
刺人心 2020-12-29 15:46

So I’ve been looking for a way to change the Stock Availability back to In Stock when the quantity field is greater than 0. The system already automatically changes the Stoc

相关标签:
5条回答
  • 2020-12-29 16:25

    Just create cron job with that code snippet:

    public function setBackInStock()
    {
        $collection = Mage::getResourceModel('cataloginventory/stock_item_collection');
        $outQty = Mage::getStoreConfig('cataloginventory/item/options_min_qty');
        $collection->addFieldToFilter('qty', array('gt' => $outQty));
        $collection->addFieldToFilter('is_in_stock', 0);
    
        foreach($collection as $item) {
            $item->setData('is_in_stock', 1);
        }
        $collection->save();
    }
    

    Small note, you can set cron job for every minute, because if there are no results job won't be consuming time/resources

    0 讨论(0)
  • 2020-12-29 16:27

    I believe you can use Magento event catalog_product_save_after. Create an observer method that does the following on event catalog_product_save_after.

    public function catalog_product_save_after($observer) {
        $product = $observer->getProduct();
        $stockData = $product->getStockData();
    
        if ( $product && $stockData['qty'] ) {
            $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId()); // Load the stock for this product
            $stock->setData('is_in_stock', 1); // Set the Product to InStock                               
            $stock->save(); // Save
        }
    }
    
    0 讨论(0)
  • 2020-12-29 16:29

    This is what I had to do

            var stock_data = new catalogInventoryStockItemUpdateEntity()
            {
                qty = quantity,
                is_in_stock = inStock,
                manage_stock = stockManaged,
                is_in_stockSpecified = true,
            };
    

    is_in_stockSpecified is important

    I used SOAP API to update stock status.

    0 讨论(0)
  • 2020-12-29 16:32

    The Stock Availability text can be handled in the Inventory Tab of the Product settings, same tab as the stock quantity field. Since you have to input your stock qty manually anyway, I would suggest to just change the Stock Availability setting back to 'In Stock' as you enter the new QTY for your product.

    0 讨论(0)
  • 2020-12-29 16:34

    Another simple solution is to create a stored procedure on DB and call it with with an event

    ############################################# START : Enable Stock Status
    DELIMITER //
    CREATE PROCEDURE EnableStock()
    BEGIN
    
    -- UPDATE
    UPDATE cataloginventory_stock_status
    SET stock_status=1
    WHERE qty>0;  
    
    -- UPDATE
    UPDATE cataloginventory_stock_status
    SET stock_status=0
    WHERE qty<0;  
    
    END; //
    DELIMITER ;
    
    ############################################# END : Enable Stock Status
    
    #Create event
    CREATE EVENT CallEnableStock
        ON SCHEDULE EVERY 1 HOUR
        DO
          CALL EnableStock();
    
    0 讨论(0)
提交回复
热议问题