1

I am trying to get the stock status of all products within a particular category. My current code is below but getIsInStock is not part of the default collection. How can i get this value in the same way as ->addAttributeToSelect('product_type')

$collection = Mage::getModel('catalog/category')->load($_cat_id)->getProductCollection()
->addAttributeToSelect('product_type');

foreach ($collection as $product) {
    if( $product->getIsInStock() ) { 
        print $product->getProductType().' is in stock';
    }
}
1

Try the below code

$collection = Mage::getModel('catalog/category')->load($_cat_id)->getProductCollection()
->addAttributeToSelect('product_type');

foreach ($collection as $product) {
  $prod=Mage::getModel('catalog/prodcut')->load($product->getId());
  if( $prod->getIsInStock() ) { 
    print $prod->getProductType().' is in stock';
 }
}

why we are crating a new object of catalog because when we fetch the products form the category then we got limited attributes so we need to load product again.

0
1
<?php
$category = Mage::getModel('catalog/category')->load($_cat_id);
$collection = $category->getProductCollection()->addAttributeToSelect('product_type');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
foreach ($collection as $product) {
    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIs_in_stock();
    if($stock == 1 && $product->isAvailable() == 1)
        print $product->getProductType().' is in stock';
}
?>

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.