How to check if there is a finished product?

We all know that a configurable work in magento is associated with a simple work.

If simple products associated with a configurable product become Inventory = 0, this means that the configurable product is not in stock

So the question is, how can I determine if a given product is in stock? I want to detect, so I can display the text "Out of stock" in the interface.

something like that

if($configurable_product->isOutOfStock()) { echo "Out of Stock"; } 

How can I do this in Magento?

+5
source share
5 answers
 if (!$configurable->isSaleable() ||$configurable_product->getIsInStock()==0){ // out of stock } 

To verify a child simple product:

 $allProducts = $configurable->getTypeInstance(true) ->getUsedProducts(null, $configurable); foreach ($allProducts as $product) { if (!$product->isSaleable()|| $product->getIsInStock()==0) { //out of stock for check child simple product } } 
+6
source
 $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku); $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId()); $qty = $stockItem->getData('qty'); $inStock = $stockItem->getData('is_in_stock'); if ($qty < 1 || $inStock == 0) { // OutOfStock } 

I prefer to double-check with qty, because in qty == 0 products will not always be in stock, depending on the configuration settings.

0
source
 $_productCollection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('type_id', array('eq' => 'configurable')); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection); 

Only custom products that are in stock are displayed here.

0
source

Just a small update / correction of Quovadisqc's answer. When defining $ qty, it should be

 $qty = $stockItem->getData('qty'); // correct 

Instead of what currently exists,

 $qty = $stockItem->setData('qty'); // incorrect 

I would post this as a comment, but I don't have enough reputation.

0
source

In the foreach product loop, the following if statement is executed.

 if ($product->getIsInStock() === '1' && $product->isSaleable() === true) { echo 'this product is in stock'; } 
0
source

Source: https://habr.com/ru/post/1200391/


All Articles