2011-09-09 9 views
1

私は、次のコードを使用して、特定のカテゴリの子カテゴリのリストを取得することができます。Magentoでは、管理領域に表示されている順序でカテゴリのリストを取得するにはどうすればよいですか?

public function displaycats($data, $begin,$end, $catid){ 
    $currentCat = Mage::getModel('catalog/category')->load($data[0]); 
    $children = explode(",",$currentCat->getChildren()); 

    foreach ($children as $child) { 
    $cot++; 
    $subCat = Mage::getModel('catalog/category')->load($child); 

     if ($cot >= $begin && $cot <= $end){ 
      echo '<a href="'.$subCat->getUrl().'?stockable=786">'.$subCat->getName()."</a><br>"; 
     } 
    } 
    return; 
} 

問題は、Magentoのは、管理エリアに注文したカテゴリを持っていると、リストが同じことを命じていないことです。誰か助けてくれますか?

答えて

6

私は@Joe定数の答えを好きですが、私はそれをしようとしたとき、私だけでも0$recursionLevel設定して、子カテゴリの最初のレベルを得るトラブルを抱えていました。そして、あなたがコレクションを反復処理し、このように必要なものは何でも行うことができます

public function getChildrenCollection($parentId=false, $sort='ASC', $attribute='position') 
{ 
    if (empty($parentId) || !is_numeric($parentId)) return false; 
    $childrenArray = explode(',',Mage::getModel('catalog/category')->load($parentId)->getChildren()); 
    // remove parent id from array in case it gets returned 
    if ($key = array_search($parentId, $childrenArray)) { 
    unset($childrenArray[$key]); 
    } 
    $collection = Mage::getModel('catalog/category')->getCollection() 
    ->addAttributeToFilter('entity_id', array('in' => $childrenArray)) 
    ->addAttributeToSelect('*'); 

    if (!empty($sort)) { 
    return $collection->setOrder($attribute, $sort); 
    } 
    return $collection;   
} 

::回避策として、私はこの関数を(の(ローカルバージョンでそれを置く)あなたがソートされた子のコレクションを必要とする方のブロック)を書いた

foreach (getChildrenCollection($parentCategoryId) as $child) { 
    Zend_Debug::dump($child->getData()); 
} 

希望します。

2

getCategoriesをご覧ください。ソートされたパラメータとコレクションオブジェクトとして返すオプションがあり、データベースから各カテゴリを再度読み込む必要がなくなります。

/** 
* Retrieve categories 
* 
* @param integer $parent 
* @param integer $recursionLevel 
* @param boolean|string $sorted 
* @param boolean $asCollection 
* @param boolean $toLoad 
* @return Varien_Data_Tree_Node_Collection|Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection 
*/ 
public function getCategories($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true) 

`

2
$helper  = Mage::helper('catalog/category'); 
    $_categories  = $helper->getStoreCategories('path', true, FALSE); 

次の形式

カテゴリ1

---サブ区分1A

------サブカテゴリー1AA

にカテゴリを表示する場合

---サブカテゴリ1b

カテゴリ2

---サブカテゴリー2A

---サブカテゴリー2B

foreach($_categories as $category){ 
     $level = $category['level'] - 2; 
     $pad = str_repeat("---", ($level > 0) ? $level : 0); 

     echo $pad . ' ' . $cat['name']); 
     echo '<br/>'; 
    } 

    return $categories; 
2

私は管理者のようにソート(PARENTIDにより)カテゴリの一覧を取得するには、このような何かを書いた:

$subCats = array(); 
$children = Mage::getModel('catalog/category')->getCategories($parentId, 1, true, true); 
foreach ($children as $category) 
{ 
    $subCats[$category->getPosition()] = $category->getId(); 
} 
ksort($subCats); 
関連する問題