0

I am creating a page that display in stock products. I am using

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

in my custom product list block extending Mage_Catalog_Block_Product_List. But this code includes products that have qty=0. If i added filter qty>0, it will also remove configurable product that have qty=0 BUT have simple(child,associated) products with qty > 0. How do I solve this?

Basically, from this current list, i want to filter out simple product that have qty < 1.

1 Answer 1

2

I solved this by adding

            if($isReadystock){
            $this->_productCollection->addAttributeToFilter(
                'status',
                array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
            );
            $this->_productCollection->getSelect()->columns(
                array(
                    'qty1' => 'IFNULL(inventory1.qty,0)',
                    'qty2' => 'IFNULL(inventory2.qty,0)',
                    'qty'  => '(IFNULL(inventory1.qty,0)  + IFNULL(inventory2.qty,0) )',
                )
                );
            $this->_productCollection->getSelect()->joinLeft(array("inventory2" => 'cataloginventory_stock_item'), "e.entity_id = inventory2.product_id", null);
            $subquery = "SELECT `cpr`.`parent_id`,  `at_inventory_in_stock`.`qty`  from `catalog_product_relation` AS `cpr` LEFT JOIN `cataloginventory_stock_item`  AS `at_inventory_in_stock` ON `at_inventory_in_stock`.`product_id`=`cpr`.`child_id` INNER JOIN `catalog_product_entity_int` AS `at_status` ON `at_status`.`entity_id`=`cpr`.`child_id` AND `at_status`.`attribute_id`=96 WHERE qty>0 AND `at_status`.`value`=1 GROUP BY parent_id";
            $this->_productCollection->getSelect()->joinLeft(array("inventory1" => new Zend_Db_Expr('('.$subquery.')')), "e.entity_id = inventory1.parent_id", null);
            $this->_productCollection->getSelect()->where('(IFNULL(inventory1.qty,0)  + IFNULL(inventory2.qty,0) )>0');
        }
1
  • You should mark it as correct answer if this is correct for you
    – seb
    Commented Mar 16, 2017 at 4:49

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.