phpmagento-1.7

Hide Bundle Product if one of the Children is outofstock


how can i filter product collection so it will return the collection which does not contain bundle product whose one of children is Out of stock.


Solution

  • Alternate answer

    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
            $otherProductIds = $collection->getAllIds();
    
            //get only bundle
            $collection = Mage::getResourceModel('catalog/product_collection')
                                            ->addAttributeToFilter('type_id','bundle');
            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
            $bundleIds = $collection->getAllIds();
    
    
            //checking bundle associate product stock status
            $readAdapter = Mage::getSingleton('core/resource')->getConnection('core_read');
    
            $select = $readAdapter->select()
                        ->from(array('css'=> Mage::getSingleton('core/resource')->getTableName('cataloginventory/stock_status')),array())
                        ->join(
                            array('bs'=> Mage::getSingleton('core/resource')->getTableName('bundle/selection')),
                            'bs.product_id = css.product_id',
                            array('parent_product_id')
                        )
                        ->where('bs.parent_product_id IN (?)',$bundleIds)
                        ->where('css.stock_status = 0')
                        ->group('bs.parent_product_id');
            $excludeBundleIds = $readAdapter->fetchCol($select);//return outstock associated products parent ids
    
            $allIds =   array_merge($otherProductIds, array_diff($bundleIds,$excludeBundleIds));
    
            $collection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToFilter('entity_id',array( 'in' => $allIds))
    
                        ->addMinimalPrice()
                        ->addFinalPrice();
    
            return $collection
    

    Hope this will helpful