2011-03-23 32 views
2

我只将产品分组可见。在我的特定产品集合中,我过滤了简单的产品。结果显示在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(); 

任何帮助appreceated.Thanks。

回答

2

解决方案:2个独立的收藏,第一次简单的产品 - >得到父母的ID,第2根据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(); 

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

2

能否请您尝试以下代码:

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

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

你好OğuzÇELİKDEMİR,谢谢你的建议。我认为我现在用getParentProductIds()方法正确。在我的情况下,不能过滤type_id,因为分组的产品没有任何属性来过滤,因此我不会得到任何结果。 – Rito 2011-03-23 13:38:10