1

Hi I'm going crazy with this function. Although I added In stock Filter, it still return Not in Stock products. Where I'm wrong ?

public function getRelatedProducts($limit = false) {
        $product = Mage::registry('current_product');

        if ($category = Mage::registry('current_category')) {

        } elseif ($product) {
            $ids = $product->getCategoryIds();

            if (!empty($ids)) {
                $category = Mage::getModel('catalog/category')->load($ids[0]);
            }
        }

        if ($category) {
            if ($limit === false) {
                $limit = Mage::getStoreConfig('autorelated/general/limit');
            }

            $products = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToFilter('visibility', array(
                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                ))
                ->addAttributeToFilter('status', 1)
                ->addAttributeToFilter('is_in_stock', 1)
                ->addAttributeToFilter('qty', array("gt" => 0))
                ->addAttributeToFilter ('price', array ('gt' => 1))
                ->addCategoryFilter($category)
                ->addAttributeToSelect('*')
                ->setPageSize($limit);

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


            if ($product) {
                $products->addAttributeToFilter('entity_id', array(
                    'neq' => Mage::registry('current_product')->getId())
                );
            }

            $products->getSelect()->order('updated_at','desc');



        } else {
            return false;
        }

    return $products;
}
3
  • try to re-indexing Dec 7, 2015 at 11:09
  • I tried a lot of times, I tried to clear cache, everything. but it still doesn't work. I really don't know what to do .. I'm going crazy Dec 7, 2015 at 11:54
  • have you found solution? i am still finding. let me know if you have found solution
    – jack
    Oct 9, 2017 at 11:46

2 Answers 2

2

Try this:

Way 1:

$collection = Mage::getModel('catalog/product')
     ->getCollection()
     ->addAttributeToSelect('*')
     ->joinField('qty',
                 'cataloginventory/stock_item',
                 'qty',
                 'product_id=entity_id',
                 '{{table}}.stock_id=1',
                 'left')
     ->addAttributeToFilter('qty', array("gt" => 0));

Way 2:

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection);
2
  • it's incredible.. still doesn't work. Dec 7, 2015 at 10:46
  • have you found solution of it? i am facing same issue. i tried way 1 and way 2 to my collection but still not working.
    – jack
    Oct 9, 2017 at 11:41
0

Use below 5 line of code for in stock product collection:

$productCollection = Mage::getResourceModel('catalog/product_collection')
                    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

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

zend_debug::dump($productCollection->getData());
3
  • return array(0) { } Dec 7, 2015 at 16:09
  • My mistake. I have edited the code. Dec 8, 2015 at 6:09
  • Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection); despite this code it shows out of stock products in product collection. not removing the out of stock products
    – jack
    Oct 9, 2017 at 11:42

Your Answer

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

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