[英]Show categories and subcategories using observer in phtml
我已經設置了magento項目。 我想使用json格式的觀察者獲取類別和子類別。 我想在phtml中調用此信息並在前端顯示。
另外,當我創建一個類別和子類別時,觀察者會自動使用json數據進行更新。 此json數據應傳遞到pthml文件。
我們如何實現這一目標。
您可以嘗試此代碼,以JSON格式獲取此所有類別和子類別的列表。
function getCategoryTree($recursionLevel, $storeId = 1)
{
$parent = Mage::app()->getStore()->getRootCategoryId();
$tree = Mage::getResourceModel('catalog/category_tree');
$nodes = $tree->loadNode($parent)
->loadChildren($recursionLevel)
->getChildren();
$tree->addCollectionData(null, false, $parent);
$categoryTreeData = array();
foreach ($nodes as $node)
{
$categoryTreeData[$node->getData('entity_id')] = getNodeChildrenData($node);
}
return $categoryTreeData;
}
function getNodeChildrenData(Varien_Data_Tree_Node $node)
{
$data = array(
'title' => $node->getData('name'),
'url' => $node->getData('url_key'),
);
foreach ($node->getChildren() as $childNode)
{
if (!array_key_exists('children', $data))
{
$data['children'] = array();
}
$data['children'][$childNode->getData('entity_id')] = getNodeChildrenData($childNode);
}
return $data;
}
print_r(json_encode(getCategoryTree(3)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.