2012-03-21 3 views
2

としてはタイトルに言ったカテゴリID与えられたすべての子供たちにカテゴリを検索:Magentoの、私は私のカスタム関数を使用してこのようなものをやろうとしている

public function retrieveAllChilds($id = null, $childs = null){ 

     $childIdsArray = is_null($childs) ? array() : $childs; 
     $category = is_null($id) ? $this->getCurrentCategory() : $this->getCategoryFromId($id); 
     if (count($this->getChildrenCategories($id)) > 0) { 
      $c = count($this->getChildrenCategories($id)); 
      $tmp_array = array(); 
      foreach ($this->getChildrenCategories($id) as $category) { 
       array_push($tmp_array, $category->getId());    
      } 
      $childIdsArray = array_merge($childIdsArray, $tmp_array); 
      foreach ($this->getChildrenCategories($id) as $category){ 
       $this->retrieveAllChilds($category->getId(), $childIdsArray); 
      } 
     } 
     else{ 
      return array_unique($childIdsArray); 
     } 

     return array_unique($childIdsArray); 
} 

が、停止中かで間違った何かがありますようです終了条件関数は最初の16個の要素を正しく検索します。誰も私を助けることができますか?

答えて

7

クラスMage_Catalog_Model_Categoryには、すでに検索している機能が含まれていると思います。それはgetChildrenと呼ばれている:

public function retrieveAllChilds($id = null, $childs = null) { 
    $category = Mage::getModel('catalog/category')->load($id); 
    return $category->getChildren(); 
} 

子供のIDをカンマで区切った、Mage_Catalog_Model_Categoryインスタンスの配列を返すgetChildrenCategoriesを返すgetChildren機能。

あなたは再帰的に子どものカテゴリを取得したい場合は、あなたが使用することができます。

public function retrieveAllChilds($id = null, $childs = null) { 
    $category = Mage::getModel('catalog/category')->load($id); 
    return $category->getResource()->getChildren($category, true); 
} 
+3

ことを除いて、デフォルトでは、それは(だけなので、トップレベル)を再帰的にそれらをフェッチしません。あなたは、正確に正しいです。彼は次のものを使いたいでしょう:$ category-> getResource() - > getChildren($ category、true); –

+0

まさに@DanielSloofです。ちょうど私の答えにそれを加えました。 – Simon

+0

tnxみんな。私は同様の方法でそれをやった – Luke

関連する問題