【问题标题】:Generate JSON string of Magento's categories and children categories from PHP从 PHP 生成 Magento 的类别和子类别的 JSON 字符串
【发布时间】:2017-06-01 15:15:54
【问题描述】:

我正在尝试为我的所有类别、子类别和子子类别生成一个包含 id 和 url 的 JSON 字符串。下面的代码只能让我到达顶级类别,但我也需要第二和第三。我的目标是创建一个动态更新的站点地图,对字符串进行解码并制作每个类别级别的无序列表。

public function createCategoryTree() {
$_categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter()->addLevelFilter(2)->addOrderField('position'); // load all categories
$arr = array();
if ($_categories){ // if there are categories
  foreach ($_categories as $cat){
    $cat_name = $cat->getName(); // name as key
    if (strlen(stristr($cat_name,'default')) < 3) {
      $cat_id = $cat->getId(); // id
      $cat_url = $cat->getUrl(); // url
      $arr[$cat_id] = array('title'=>$cat_name,'url'=>$cat_url); 
    }
  }
}
print_r (json_encode($arr));
die;
}

我得到这样的东西:

{
    "637": {
        "title": "bikes",
        "url": "http:www.123.com/shop/bicycles "
    }
}

但我想要这样的东西:

{
    "637": {
        "title": "bikes",
        "url": "http:www.123.com/shop/bicycles",
        "children": {
            "658": {
                "title":"wheels",
                "url":"http:www.123.com/shop/bicycles/wheels"
            },
            "489": {
                "title": "pumps",
                "url":"http:www.123.com/shop/bicycles/pumps"
            }
    }
}

感谢您的时间和帮助!

【问题讨论】:

    标签: php json magento


    【解决方案1】:

    您可以为此使用Mage_Catalog_Model_Resource_Category_Tree 提供的一些功能。看看这个脚本是否适合你:

    <?php
    require_once('../app/Mage.php');
    Mage::app();
    
    function getCategoryTree($recursionLevel, $storeId = 1)
    {
        $parent = Mage::app()->getStore()->getRootCategoryId();    
        $tree = Mage::getResourceModel('catalog/category_tree');
        /* @var $tree Mage_Catalog_Model_Resource_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)));
    

    【讨论】:

    • 非常感谢!我还没有让它完全按照我的需要工作,但是你让我走上了一条理解如何更好地到达那里的道路,我觉得现在离让它工作很近了。我很感激!
    • 很高兴能帮上忙! - 也许您应该接受其他有类似问题的人的答案,或者告诉我们您还缺少什么,以便我们继续为您提供帮助。
    • 知道了!抱歉,Stack 还是新手。我将梳理出其中的一些语法错误,并在几个小时后回到这里解决我仍有的任何问题。
    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2011-11-05
    • 1970-01-01
    相关资源
    最近更新 更多