2011-09-09 5 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; 
} 

문제는 마 젠토는 관리 영역에 정렬 카테고리를 가지고 같은 목록이 동일하게 정렬되지 않는 것입니다. 누구든지 나를 도울 수 있습니까?

답변

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); 
관련 문제