簡體   English   中英

獲取兒童類別magento

[英]Get Child categories magento

試圖獲得活躍的特定類別的孩子。 請幫忙。 我無法做到這一點。 我現在能夠向他們展示所有但不具體。 非常感謝任何幫助。

$category = Mage::getModel('catalog/category')->load(2);
$category->getChildCategories();
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();

這是加載活動類別的代碼

/* Load category by id*/
$cat = Mage::getModel('catalog/category')->load($id);


/*Returns comma separated ids*/
$subcats = $cat->getChildren();

//Print out categories string
#print_r($subcats);

foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive())
  {
    $caturl     = $_category->getURL();
    $catname     = $_category->getName();
    if($_category->getImageUrl())
    {
      $catimg     = $_category->getImageUrl();
    }
    echo '<h2><a href="'.$caturl.'" title="View the products for this category"><img src="'.$catimg.'" alt="" />'.$catname.'</a></h2>';
  }
}
?>

希望這對你有所幫助。

正如mhaupt所提到的,加載集合而不是循環中的每個類別更快。 但是,就我而言,沒有必要手動加載子類別。 基本上這就是$category->getChildrenCategories()已經做到的。

還有一個過濾器只能獲取活動類別。 只需在集合上調用addIsActiveFilter()

a。)通過getChildren()加載活動的子類別

// 1. Get a list of all child category ids (e.g "12,23,11,42")
$subcategoryIds = $category->getChildren();

// 2. Create collection
$categoryCollection = Mage::getModel('catalog/category')->getCollection();

// 3. Add all attributes to select, otherwise you can not 
//    access things like $cat->getName() etc.
$categoryCollection->addAttributeToSelect('*');

// 4. Filter by ids
$categoryCollection->addIdFilter($subcategoryIds);

// 5. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();

b。)使用getChildrenCategories()加載活動子類別

// 1. Load collection
$categoryCollection= $category->getChildrenCategories();

// 2. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();

一旦訪問該集合,就會從數據庫中加載該集合。 如果未加載集合並且調用$subcategories->count()則僅針對數據庫觸發“SELECT count(*)”(與count($subcategories)相反,這將強制集合加載自身)。

迭代集合

foreach($categoryCollection as $category) {
    echo $category->getName();
}

如果在訪問集合后向集合添加更多過濾器,則集合將不會自動再次加載。 要將更改應用於集合,只需調用$categoryCollection->load()從數據庫重新加載集合。

那些說使用getAllChildren()而不是getChildren()的人完全錯了。 兩種方法都返回完全相同的東西,但有一點不同,getAllChildren(true)將返回一個數組而不是逗號分隔的字符串。 getAllChildren($ bool asArray)默認為false。 我的觀點是,無論哪種方式,你都必須使用

Mage::getModel('catalog/category')->load($catId);

除非你使用下面的函數,否則在循環內部。

private function fetchCatsById($onlyThese)
{
    $cats = Mage::getModel('catalog/category')
                ->getCollection(true)
                ->addAttributeToSelect('*')
                ->addIdFilter($onlyThese)
                ->addAttributeToFilter('level','2')
                ->addIsActiveFilter();

    return $cats;
}

$cats = $this->fetchCatsById($onlyThese);

liyakat寫的一個答案,不應該在專業商店中使用,因為它會引發性能問題,因為類別對象有多次n次加載,而是使用類別集合,讓所有孩子

$cat->getAllChildren()

,然后通過所需的類別ID限制類別集合

$coll->addIdFilter($idFilter);

那么您將不必對數據庫加載n次。

請記住,循環中的加載是任何Magento項目中最常用的錯誤代碼示例之一,並避免它們!

你好,你會看到下面的代碼

$category_model = Mage::getModel('catalog/category'); 
  $_category = $category_model->load(13); 
  $all_child_categories = $category_model->getResource()->getAllChildren($_category);
  print_r($all_child_categories);

如果你想要任何數量的父類別子類別而不是點擊這里http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM