2010-08-12 118 views
1

我已經triyng更改默認的默認結果頁面,我希望產品按類別分組,它不包括子類別,但產品,搜索條件是產品名稱,所以,我triyng使用defalt簡單搜索magento,直到現在沒有結果,也許我必須重寫搜索,並作出新的,我知道這個我可以得到所有的類別和其產品收集,Magento搜索結果以我的方式

$categories = Mage::getModel('catalog/category') 
->getCollection() 
->addAttributeToSelect('*');  

foreach($categories as $category) 
{ 
    $array = $category->getParentIds(); 
    $children = explode(',',$category->getChildren()); 
    $products = $category->getProductCollection(); 
} 

,但我需要通過produc名稱進行篩選,我secting顯示結果是這個樣子

Category I 
    - Product I 
    - Product II 
Category II 
    - Product III 
    - Product IV 
+0

那麼你在問什麼?你到目前爲止試過什麼改變? – 2010-08-12 18:43:46

回答

1

編碼後的幾天裏,我能得到與孩子的產品類別,我想分享我的代碼清單,也許這將幫助別人:

public function getProducts(){ 
    $categories=Mage::getModel('catalog/category')->getCollection(); 
    $result; 
    foreach($categories as $cat) 
    { 
     $temp = null; 
     $_temp = null; 
     $are = false; 
     $_cat = $cat->load(); 
     $temp['category'] = $_cat->getName(); 
     $prod = $_cat->getProductCollection() 
        ->addAttributeToFilter('name', array('like'=>'%'.$this->getRequest()->getParam('q').'%')); 
     foreach($prod as $p){ 
      //die(print_r($p->load())); 
      $_temp[] = $p->load(); 
      $are = true; 
     } 
     if($are){ 
      $temp[] = $_temp; 
      $result[] = $temp; 
     } 
    } 
    return $result; 
} 

我把這個功能在我的自定義模塊的模塊,該功能在每個位置與另一個數組返回一個數組,然後在.phtml文件,你可以這樣

<?php $productsResult = $this->getProducts(); 
if(count($productsResult)+count($prodnorelatedResult)>0){ ?> 
    <h2>Products</h2> 
     <?php foreach($productsResult as $p){ ?> 
    <h3><?php echo $p['category'] ?></h3> 
    <?php foreach($p[0] as $_p){ ?> 
     <div><a href="<?php echo $_p->getProductUrl()?>"><?php echo $_p->getName() ?></a></div><br/> 
    <?php } ?> 
    <br/> 
<?php } } ?> 

這裏循環是怎麼顯示的類別與名稱的例子他的產品名稱作爲產品詳細信息頁面的鏈接

希望它有幫助!