【问题标题】:Create new magento Rest api to get category list in magento创建新的 magento Rest api 以获取 magento 中的类别列表
【发布时间】:2016-05-17 21:32:15
【问题描述】:

有谁知道如何创建新的 magento rest api 来获取类别列表。

我已经看到了这个链接 magento rest api 但这不会获取类别列表。

如果有人知道,请解决这个问题..

【问题讨论】:

    标签: php api magento rest oauth


    【解决方案1】:

    首先使用http://www.silksoftware.com/magento-module-creator/ this 创建基本模块,然后您必须创建以下文件并将此文件添加到给定路径中。

    模块路径 = app/code/local/Restapi/Categories

    1) app/code/local/Restapi/Categories/etc/api2.xml

    <?xml version="1.0"?>
    <config>
    <api2>
        <resource_groups>
            <catalog translate="title" module="api2">
                <title>Catalog</title>
                <sort_order>10</sort_order>       
            </catalog>
        </resource_groups>
        <resources>
            <categories translate="title" module="api2">
                <group>catalog</group>
                <model>categories/api2_category</model>
                <title>Categories</title>
                <sort_order>10</sort_order>
                <privileges>
                    <admin>
                        <retrieve>1</retrieve>
                  <!--  <create>1</create>
                        <update>1</update>
                        <delete>1</delete>  --> 
                    </admin>
                    <customer>
                        <retrieve>1</retrieve>
                  <!--  <create>1</create>
                        <update>1</update>
                        <delete>1</delete>  --> 
                    </customer>
                    <guest>
                        <retrieve>1</retrieve>
                  <!--  <create>1</create>
                        <update>1</update>
                        <delete>1</delete>  --> 
                    </guest>
                </privileges>
                <attributes>
                    <category_id>Category ID</category_id>
                    <name>Name</name>
                    <parent_id>Category Parent ID</parent_id>
                    <child_id>Category Child List</child_id>
                    <active>Active</active>
                    <level>Level</level>
                    <position>Position</position>
                </attributes>
                <routes>
                    <route>
                        <route>/categories/:cat_id</route>
                        <action_type>collection</action_type>
                    </route>
                </routes>
                <versions>1</versions>
            </categories>
        </resources>
    </api2>
    </config>
    

    这里的 cat_id 是我们传递给获取子类别的类别 ID。

    2) app/code/local/Restapi/Categories/Model/Api2/Category/Rest/Customer/V1.php

    <?php
    class Restapi_Categories_Model_Api2_Category_Rest_Customer_V1 extends Restapi_Categories_Model_Api2_Category
    {
    /**
     * Retrieve list of category list.
     *
     * @return array
     */
    protected function _retrieveCollection()
    {   
        $ruleId = $this->getRequest()->getParam('cat_id');
    
    //  $cat_mod = Mage::getModel('catalog/category')->load($ruleId)->toArray();
        $cats = Mage::getModel('catalog/category')->load($ruleId);
        $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getChildren();
    
        $cur_category = array();
    
        $node['category_id'] = $ruleId;
        $node['name'] = $cats->getName();
        $node['parent_id'] = $cats->getParentId();
        $node['child_id'] = $subcats;
        if($cats->getIsActive()){
            $node['active'] = 1;
        }else{
            $node['active'] = 0;
        }
        $node['level'] = $cats->getLevel();
        $node['position'] = $cats->getPosition();
    
        $cur_category[] = $node;
    
    //  $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getAllChildren();
    //  $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getChildren();
    
        if($subcats != '') 
        {
            foreach(explode(',',$subcats) as $subCatid)
            {
                $_category = Mage::getModel('catalog/category')->load($subCatid);
                $childcats  = Mage::getModel('catalog/category')->load($subCatid)->getChildren();
    
                $node['category_id'] = $subCatid;
                $node['name'] = $_category->getName();
                $node['parent_id'] = $_category->getParentId();
                $node['child_id'] = $childcats;
                if($_category->getIsActive()){
                    $node['active'] = 1;
                }else{
                    $node['active'] = 0;
                }
                $node['level'] = $_category->getLevel();
                $node['position'] = $_category->getPosition();
    
                $cur_category[] = $node;
    
            }
        }
    
        return $cur_category;       
    }
    

    如果您有 admin 和 guest 等其他用户,您也可以在此路径中添加相同的 V1.php 文件。

    app/code/local/Restapi/Categories/Model/Api2/Category/Rest/Admin/V1.php

    app/code/local/Restapi/Categories/Model/Api2/Category/Rest/Guest/V1.php

    只是您想更改 V1.php 文件中的类路径,例如 Customer = Admin。

    【讨论】:

    • 嘿,我遇到了完全相同的问题,并在 magento CE 1.9 中尝试了您的解决方案,但它不起作用。相反,我收到以下错误:错误 404,消息:“请求不匹配任何路由。”有什么帮助吗?
    • 上一个错误已解决,但仍未解决。现在出现错误消息:“找不到资源”。
    • 将您的代码发送至 bharat.chodvadiya@gmail.com。我会检查的。
    • 谢谢,您的示例根本不完整:)。我还没有创建类Restapi_Categories_Model_Api2_Category
    • Restapi_Categories_Model_Api2_Category 类里面有什么? REST URL 是什么?
    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2014-08-20
    相关资源
    最近更新 更多