2

I want to show only products in stock, how to do this with following Magento code?

$category_model = Mage::getModel('catalog/category')->load(55);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category_model);

How to add this filter to above code?

3 Answers 3

4

Add below code to filter the collection

$cond = array(
    '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
    '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0',
);


    $cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';


$collection->joinField(
    'inventory_in_stock',
    'cataloginventory/stock_item',
    'is_in_stock',
    'product_id=entity_id',
    '(' . join(') OR (', $cond) . ')'
);
2
$collection->addStoreFilter()

should do the trick, at least if you did not configure Magento to show out of stock products in the frontend. This is filtering the collection to only show products available in the current store.

0

This worked for me:

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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