2011-03-23 54 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