0
我已经安装了magento项目。我想使用json格式的观察者来获取类别和子类别。这个信息我想在phtml中调用并显示在前端。在phtml中使用观察者显示类别和子类别
此外,当我创建一个类别和子类别时,观察者会自动更新json数据。这个json数据应该传递给pthml文件。
我们该如何实现这一点。
我已经安装了magento项目。我想使用json格式的观察者来获取类别和子类别。这个信息我想在phtml中调用并显示在前端。在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)));