隐藏捆绑产品,如果其中一个孩子是缺货

时间:2013-08-30 09:00:34

标签: magento-1.7

如何过滤产品系列,以便返回不包含其中一个儿童缺货的捆绑产品的集合。

2 个答案:

答案 0 :(得分:1)

得到了我的问题的解决方案

$collection = Mage::getResourceModel('catalog/product_collection');



        $bundled_items = array();
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        foreach ($collection->getAllIds() as $proId)
        {
            $bundled_product=Mage::getModel('catalog/product')->load($proId);
            if($bundled_product->getTypeId()=="bundle")
            {
                $selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
                $bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
                );
                foreach($selectionCollection as $option)
                {
                    $product = Mage::getModel('catalog/product')->load($option->getProductId());
                    $stockItem = $product->getStockItem();
                    if($product->stock_item->is_in_stock == 0)
                    {
                        $bundled_items[] = $proId;
                    }
                }

            }
        }
        if(isset($bundled_items) && !empty($bundled_items))
        {
            $collection->addFieldToFilter('entity_id',array( 'nin' => array_unique($bundled_items)));
        }

答案 1 :(得分:1)

替代回答

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

希望这会有所帮助