Magento: Automatically change "stock availability" from "Out of stock" to "In stock" (and vice versa) to change quantity

So, I was looking for a way to change the availability of stocks in a warehouse when the quantity field is greater than 0. The system already automatically changes the availability of stock in the warehouse after setting the quantity to 0 and saving the product. I would like to return it in stock when you set the quantity is greater than 0 and save the product.

Well, I think I found a simple way that in itself makes me nervous. Therefore, I wanted to publish a guru to you to make sure that it is safe, correct and normal to do it.

In app / design / adminhtml / default / default / template / catalog / product / tab / inventory.phtml

I changed this:

<?php foreach ($this->getStockOption() as $option): ?> <?php $_selected = ($option['value'] == $this->getFieldValue('is_in_stock')) ? 'selected="selected"' : '' ?> <option value="<?php echo $option['value'] ?>" <?php echo $_selected ?>><?php echo $option['label'] ?></option> <?php endforeach; ?> 

For this:

 <?php if( ($this->getFieldValue('qty')*1) > 0): ?> <option selected="selected" value="1">In Stock</option> <?php else: ?> <option selected="selected" value="0">Out of Stock</option> <?php endif; ?> 

All I have to work on at the moment is a live site, so you can understand my concern ...

Please let me know if this will have the intended effect (it seems so, but it seems simplified ....)

+7
source share
4 answers

I believe you can use Magento event_product_save_after. Create an observer method that does the following in event_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 } } 
+6
source

Just create a cron job with this piece of code:

 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(); } 

A short note: you can set the cron task every minute, because if there are no results, the task will not consume time / resources

+1
source

The text “Stock Availability” can be processed on the “Resources” tab of the “Product” parameters, on the same tab as the stock quantity field. Since you still have to enter your stock manually, I would suggest simply changing the "Stock Availability" option to "In Stock" when you enter a new QTY for your product.

0
source

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 important

I used the SOAP API to update the stock status.

enter image description here

0
source

All Articles