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

问题描述 投票:0回答:2

我如何过滤产品集合,以便它返回不包含其中一个子项缺货的捆绑产品的集合。

php magento-1.7
2个回答
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
投票

替代答案

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

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.