如何将分组产品集成到集合中

时间:2011-03-23 10:38:39

标签: php collections magento

我只有可见的分组产品。在我的特定产品系列中,我过滤了简单的产品。结果显示在list.phtml中,但作为简单的产品。我需要的是与简单产品相关的分组产品,而不是listview中显示的简单产品。我无法弄清楚Magento目录是如何处理这个问题的。

PHP:

public function getMySpecialProducts($maxlength=null,$minlength=null,$maxwidth=null,$minwidth=null,$maxheight=null,$minheight=null){

        $products = Mage::getModel('catalog/product');
        $_collection = $products->getCollection();
        $_collection->addAttributeToSelect('*')
        ->addAttributeToFilter('size_length', array('lteq' => ''.$maxlength.''))
        ->addAttributeToFilter('size_length', array('gteq' => ''.$minlength.''))
        ->addAttributeToFilter('size_width', array('lteq' => ''.$maxwidth.''))
        ->addAttributeToFilter('size_width', array('gteq' => ''.$minwidth.''))
        ->addAttributeToFilter('size_height', array('lteq' => ''.$maxheight.''))
        ->addAttributeToFilter('size_height', array('gteq' => ''.$minheight.''))
        ->setPage(1, 10)
        ->addCategoryFilter($this->getMySpecialCategory())
        ->load();

        return $_collection;
    }

phtml输出:

$_productCollection = $this->getMySpecialProducts(
    $range["length"]["max"],$range["length"]["min"],
    $range["width"]["max"],$range["width"]["min"],
    $range["height"]["max"],$range["height"]["min"]
);

$listView = $this->getLayout()->createBlock('catalog/product_list')
            ->setTemplate('catalog/product/list.phtml')
            ->setCollection($_productCollection);
$this->getLayout()->getBlock('content')->append($listView);
echo $listView->toHTML();

任何帮助都被激活了。谢谢。

2 个答案:

答案 0 :(得分:2)

请您试试以下代码:

$collectionGrouped = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'grouped'));

// Get the grouped product a simple product belongs to
$simpleProduct->loadParentProductIds();
$parentProductIds = $simpleProduct->getParentProductIds();

$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('type_id', array('eq' => 'grouped'))
->addAttributeToFilter('size_length', array('lteq' => ''.$maxlength.''))
->setPage(1, 10)
->addCategoryFilter($this->getMySpecialCategory())
->load();

答案 1 :(得分:2)

解决方案:2个单独的集合,第1个简单的产品 - >获取父ID,第二个根据ID获得分组。

以下内容添加到上面的代码中。 PHP:

public function getGroupCollection($groupId){
    $str = array();
    foreach($groupId as $value){
        foreach($value as $id){
            array_push($str, array('attribute'=>'entity_id', 'eq' => ''.$id.''));
        }
    }

    $products = Mage::getModel('catalog/product');
    $_groupCollection = $products->getCollection();
    $_groupCollection->addAttributeToSelect('*');
    $_groupCollection->addAttributeToFilter('type_id', array('eq' => 'grouped'));
    $_groupCollection->addAttributeToFilter($str);
    $_groupCollection->addCategoryFilter($this->getFormatfinderCategory())
    ->load();

    return $_groupCollection;
}

前端:

// get ID's of Parent Product (grouped products) from 1st collection
$parentProductIds = array();
foreach($_productCollection as $product){
    $product->loadParentProductIds();
    array_push($parentProductIds, $product->getParentProductIds());
}

创建html块:

$listView = $this->getLayout()->createBlock('catalog/product_list')
            ->setTemplate('catalog/product/list.phtml');

$_group = $this->getGroupCollection($parentProductIds);
$listView->setCollection($_group);

$this->getLayout()->getBlock('content')->append($listView);
echo $listView->toHTML();

肯定不是最好的解决方案,对我来说它运作正常。