1

OK, so I'm in the related.phtml page and I'm trying to load in the realeted products only those who are in stock. So there was this code already in the page:

$productCollection = Mage::getResourceModel('catalog/product_collection')
       ->addCategoryFilter($category)
       ->setCurPage(0)
       ->addAttributeToSelect('*') // add all attributes - optional
       ->addAttributeToFilter('status', 1) // enabled
       ->addAttributeToFilter('visibility', 4)      
       ->setPageSize(4);

and I want to filter with the products that are in stock. So I'm adding another one filter in the code:

$productCollection = Mage::getResourceModel('catalog/product_collection')
         ->addCategoryFilter($category)
         ->setCurPage(0)
         ->addAttributeToSelect('*') // add all attributes - optional
         ->addAttributeToFilter('status', 1) // enabled
         ->addAttributeToFilter('visibility', 4)             
         ->joinField('qty',
             'cataloginventory/stock_item',
             'qty',
             'product_id=entity_id',
             '{{table}}.stock_id=1',
             'left')
         ->setPageSize(4);

but it still brings me products that are out of stock. I also try this:

$productCollection = Mage::getResourceModel('catalog/product_collection')
      ->addCategoryFilter($category)
      ->setCurPage(0)
      ->addAttributeToSelect('*') // add all attributes - optional
      ->addAttributeToFilter('status', 1) // enabled
      ->addAttributeToFilter('visibility', 4)    
     ->addAttributeToFilter('is_in_stock', array('eq' => '1'))        
     ->setPageSize(4);

but I still get product that are out of stock. What is wrong with the code?

Thank you

EDIT: I've also set the Display out of stock products to No in the Admin Panel in Catalog->Inventory

1
  • Do you have done reindexing?
    – Amit Bera
    Oct 2, 2017 at 12:13

2 Answers 2

3

Below code will return you the products of a category that have

=> status => Enable

=> Visibility => catalog,search

=> Stock Availability => In stock

    $cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId‌​(); 
   $productDetails    = Mage::getModel('catalog/category')->load($cat_id)
                          ->getProductCollection()
                          ->addAttributeToSelect('*');
   $productDetails -> addAttributeToFilter('visibility', array('in' => array(4) ));
   $productDetails -> addAttributeToFilter('status', array('in' => array(1) ));
   $productDetails->joinField('is_in_stock',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                'is_in_stock=1',
                '{{table}}.stock_id=1',
                'left');
3
  • Thank yoy.! how do I get $cat_id variable ?
    – Stefanos
    Oct 2, 2017 at 12:32
  • found it $cat_id = Mage::registry('current_category')->getId(); It's still not working..... :-(
    – Stefanos
    Oct 2, 2017 at 12:42
  • Thank for the reply. It returns the products but I was wondering if it only returns configurable products.
    – Stefanos
    Oct 3, 2017 at 14:18
0

Try this way

    $productCollection = Mage::getResourceModel('catalog/product_collection')
           ->addCategoryFilter($category)
           ->setCurPage(0)
           ->addAttributeToSelect('*') // add all attributes - optional
           ->addAttributeToFilter('status', 1) // enabled
           ->addAttributeToFilter('visibility', 4)      
           ->setPageSize(4);
    // you already have above code now add this
    $productCollection->getSelect()->join('cataloginventory_stock_status', 'cataloginventory_stock_status.product_id = e.entity_id', array('stock_status'));
    $productCollection->getSelect()->where("`cataloginventory_stock_status`.`stock_status` = 1");

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.