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?

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.

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.