Pergunta

How to get all data about the product having id including stock status and quantity?

Foi útil?

Solução

if you load the product like $product = Mage::getModel('catalog/product')->load($id);, to get the qty available for the product, you can do $qty = $product->getStockItem()->getQty();

and to check if the product is in stock:

   $isProductInStatus = $product->isInStock();

In block:

$product = Mage::getModel('catalog/product')->load($id);
$qty = $product->getStockItem()->getQty();
$isProductInStatus = $product->isInStock();

the above has nothing to do with collection and instead load function just set the product model data. As part of the product model load process, the stock item model is loaded and this is why we can query qty like I show above

Outras dicas

You asked about fetching stock as part of collection

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*');

foreach ($collection as $product) {
    Zend_Debug::dump($product->getStockItem()->debug())
    echo $product->getStockItem()->getIsInStock();
}

You can also do it this way too. Which might be more efficient. Loading large collections or products within collections is expensive in terms of resource. This has a smaller footprint.

$stockItem = Mage::getModel('cataloginventory/stock_item')
    ->loadByProduct($productId);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top