2012-10-26 51 views
2

我需要在Magento的1.7如何對Magento分組產品頁進行分類?

對於創建分組的產品頁面,我增加了一個產品作爲組合產品和相關的一些簡單的產品那裏。 分組產品在表格行中逐個顯示在前端。我的要求是在相應產品的相應類別中顯示此頁面。

例如: - 我有兩個分類餐桌餐椅

我創造了一個組合產品說林恩集餐飲,以及相關的兩個簡單的產品林恩餐桌(從餐桌類別)和林恩餐椅(從餐椅類別)。默認情況下,它將在表格行中逐個顯示。像這樣: -

Lyn Dining Table Product Attributes... 
Lyn Dining Chair Product Attributes... 
...................................... 
...................................... 
...................................... 

但我需要在相應的類別標題中顯示。那就是: -

Dining Table:- 
Lyn Dining Table Product Attributes... 
...................................... 
...................................... 

Dining Chair:- 
Lyn Dining Chair Product Attributes... 
...................................... 
...................................... 

爲此,我編輯了grouped.phtml文件。首先,我用這段代碼取得了類別名稱: -

 /** 
     * get categories from a product 
     */ 
     $categoryIds = $this->htmlEscape($_item->getCategoryIds()); 

     /** 
     * looping through the array of category ids 
     */ 
     foreach($categoryIds as $categoryId) { 
      $category = Mage::getModel('catalog/category')->load($categoryId); 
      $categoryB=$category->getName(); 
      } 

通過這個,我可以獲取類別名稱。但我不知道如何實現循環,以便所有產品都可以在相應的類別中列出。任何人都請伸出援助之手。

回答

0

我想創建一個輔助功能,像這樣(請記住,這個代碼還是有點粗糙):

公共職能formatProductsWithCategories($產品) {$ 類別=陣列(); $ categoryProducts = array(); $ hasOrphans = false;

foreach ($products as $product) { 
     $categories = array_merge($product->getCategoryIds(), $categories); 
     foreach($product->getCategoryIds() as $categoryId) { 
      $categoryProducts[$categoryId][] = $product; 
      $found = true; 
     } 

     if (!$found){ 
      $categoryProducts["empty"][] = $product; 
      $hasOrphans = true; 
     } 
    } 

    $categoryCollection = Mage::getResourceModel('catalog/category_collection') 
     ->addAttributeToFilter('entity_id', array('in', $categories)) 
     ->addAttributeToSelect('name'); 

    $categoryCollection->load(); 

    if ($hasOrphans) { 
     $categoryCollection->addItem(
      Mage::getModel('catalog/category') 
       ->addData(array(
        'id' => 'empty', 
        'name' => 'No Categories' 
      )) 
     ); // assigning orphan values 
    } 

    foreach ($categoryCollection as $category) { 
     if (array_key_exists($category->getId(), $categoryProducts)) { 
      $category->setProducts($categoryProducts[$category->getId()]); 
     } 
    } 

    return $categoryCollection; 
} 

然後,在你grouped.phtml模板,這樣做:

<?php $categories = Mage::helper('helper_module/category')->formatProductsWithCategories($_associatedProducts); ?> 
<?php foreach ($categories as $category):?> 
    <tr><td><?php echo $category->getName();?></td></tr> 
    <?php foreach ($category->getProducts() as $_item): ?> 
     // . . . Existing loop here . . . 
    <?php endforeach; ?> 
<?php endforeach; ?>