0

I am working on a store with thousands of products. I have noticed that when product Quantity is zero (or manually turned to Zero) the Stock Availability goes 'Out of Stock' however when we update the product Quantity to number greater then zero like 10,15 or 200 the 'Stock Availability' stays 'Out of Stock' unless and until manually changed to 'In Stock' so that the product appears on fronted.

I want the 'Stock Availability' to Auto turned to 'In Stock' as we update the Quantity to number greater then Zero. The Quantity in my case gets its values from the store inventory software we don't change it manually.

Event used cataloginventory_stock_item_save_commit_after

Any help and tips will be appreciated.

public function autoinstockproduct($observer) { 



   $product = $observer->getProduct();
    $stockData = $product->getStockData();

    if ( $product && $stockData['qty'] > 0) {
        $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
    }
}

But this code is not working

Reference question

  • When you update product qty 0 to more then, you need to automatically update the status out of stock to in stock correct ? – Rajan Soni Nov 14 '16 at 5:58
  • i need to auto update stock status to in stock if im update mt qty 0 to any high number – Yatin Patel Nov 14 '16 at 7:03
  • ok I provide you ans for that – Rajan Soni Nov 14 '16 at 7:04
  • OK waiting for your ans... – Yatin Patel Nov 14 '16 at 7:08
1

The event that you need to use in this case is catalog_product_prepare_save and change your observer method like this :

public function autoinstockproduct($observer) 
{
    $product   = $observer->getProduct();
    $stockData = $product->getStockData();

    //make sure stock qty is greater than zero.
    if (isset(stockData['qty']) && $stockData['qty'] > 0) {
        $product->setStockData['is_in_stock'] = 1; //update to instock
    }
}

This is more better solution than the accepted solution from your reference question because of following reasons :

  1. It listens to more specific event which will be fired for catalog product edit save action from admin side.
  2. It removes stock model loading and additional saving.

Other than this, you need to exactly follow what @quaisersatti suggests in his answer in the referenced thread in order to properly configure your observer in your module.

0

you can check this github link for complete code

 class MobWeb_UpdateStockStatus_Model_Observer
    {
        /*
         *
         * Before saving a stock item, check if the stock is > 0. If yes, set the product's
         * "Is in Stock" attribute to 1. Also check the same for the parent configurable product
         *
         */
        public function cataloginventoryStockItemSaveBefore($observer)
        {
            $log = Mage::helper('updatestockstatus/log');

            // Get the stockitem and load the current product
            $stockItem = $observer->getItem();
            $product = Mage::getModel('catalog/product')->load($stockItem->getProductId());

            if(!$product || !$stockItem) {
                $log->log('Unable to load stock item or product');
            }

            $log->log(sprintf('cataloginventoryStockItemSaveBefore called for product %s with stock quantity %s', $product->getId(), $stockItem['qty']));

            // If the product is a simple product, check its stock quantity and update the stock status accordingly
            if($product->getTypeId() === 'simple') {
                $stockStatus = ($stockItem['qty'] > 0) ? 1 : 0;
                $stockItem->setData('is_in_stock', $stockStatus);

                $log->log(sprintf('Simple product %s stock status updated to %s', $product->getId(), $stockStatus));
0

Open your module config.xml add this event between tag into tag.

<catalog_product_save_before>
   <observers>
         <catalog_product_save_after_action_handler>
                <type>model</type>
                <class>stockalert/observer</class>
                <method>updateStatus</method>
         </catalog_product_save_after_action_handler>
   </observers>
</catalog_product_save_before>

after go to your module observer file than add this code in namespace/module/Model/observer.php

 public function autoinstockproduct($observer) 
{
    $product   = $observer->getProduct();
    $stockData = $product->getStockData();

    //make sure stock qty is greater than zero.
    if (isset(stockData['qty']) && $stockData['qty'] > 0) {
        $product->setStockData['is_in_stock'] = 1; //update to instock
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.