【问题标题】:How to get grouped products into collection如何将分组产品放入集合中
【发布时间】:2011-07-21 05:02:33
【问题描述】:

我只看到分组产品。在我的特定产品系列中,我过滤了简单的产品。结果显示在 list.phtml 中,但显示为简单的产品。我需要的是与简单产品相关的分组产品,而不是列表视图中显示的简单产品。我无法弄清楚 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();

感谢任何帮助。谢谢。

【问题讨论】:

    标签: php collections magento


    【解决方案1】:

    请您尝试以下代码:

    $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();
    

    【讨论】:

    • 您好 Oğuz ÇELİKDEMİR,感谢您的建议。我认为我现在使用 getParentProductIds() 的方式是正确的。在我的情况下,无法过滤 type_id,因为分组产品没有任何要过滤的属性,因此我不会得到任何结果。
    【解决方案2】:

    解决方案:2 个单独的集合,第一个简单产品 -> 获取父 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();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多