【问题标题】:ASC and DESC conditions in SOAP API magento 1.9SOAP API magento 1.9 中的 ASC 和 DESC 条件
【发布时间】:2017-11-10 23:16:48
【问题描述】:

我已经重写了 Magento Core (M1.9) 的 Api 模型,并想测试它是如何工作的。它也返回可能的记录,我想在我的测试中设置像 Sql "order BY Desc|Asc" 和 "LIMIT" 这样的条件。但我不知道我应该把提到的条件放在哪里。 这是我的测试代码:

$username = 'testapi';
$apikey= 'password';

$client = new Zend_XMLRPC_Client('https://www.magentohost.com/index.php/api/xmlrpc');
$session = $client->call('login', array($username, $apikey));
$filters = array(
    array(
        'category_id' => 163,
        'internal_rating' => 6
    //array('product_id'=>'Order by ASC')
));

try {
    $message = $client->call('call', array($session, 'catalog_product.list', $filters));

        var_dump($message);

} catch (Exception $fault) {
    echo $fault->getMessage();
}

如果有任何建议,我将不胜感激

【问题讨论】:

    标签: magento soap


    【解决方案1】:

    请尝试测试以下代码:

    $filters = array(
        array(
            'category_id'     => 163,
            'internal_rating' => 6, 
            'order'           => 'product_id',
            'dir'             => 'asc',
            'limit'           => 100    
    ));
    

    我自己没有测试过。我从链接http://devdocs.magento.com/guides/m1x/api/rest/get_filters.html中获取了过滤器参数的格式 也许它也适用于您的情况。

    【讨论】:

    • 谢谢阿列克斯。我以前试过这种方法,但不成功。
    【解决方案2】:

    你必须重写这个类并覆盖 items 函数 class= Mage_Catalog_Model_Product_Api 功能 = 项目

      public function items($filters = null, $store = null, $extra = [])
        {
            $collection = Mage::getModel('catalog/product')->getCollection()
                ->addStoreFilter($this->_getStoreId($store))
                ->addAttributeToSelect('name');
    
            if(isset($extra["cur_page"])) {
                $collection->setCurPage($extra["cur_page"]);
            }
            if(isset($extra["page_size"])) {
                $collection->setPageSize($extra["page_size"]);
            }
            if(isset($extra["order"])) {
                $collection->setOrder($extra["order"]["field"], $extra["order"]["type"]);
            }
    

    然后你可以调用它

    $filters = []; 
    $extra = ["cur_page"=>1,"page_size"=>"3","order"=>["field"=>"name", "type"=>"desc"]];
    
    $result= $proxy->call($sessionId, 'catalog_product.list',[$filters,null,$extra]);
    

    【讨论】:

    • 感谢 HAKIM 的回答,我没有尝试您的解决方案,但我认为它会起作用。正如我在回答中描述的那样,我解决了我的问题,这与您给出的方式相似。了解您对我的看法很有趣。
    【解决方案3】:

    我尝试通过GET方法将参数limit,dir,order转换为url请求:

    $client = new 
    Zend_XMLRPC_Client('https://www.magentohost.com/index.php/api/xmlrpc?
    limit=1&dir=desc&order=created_at');
    
    $session = $client->call('login', array($username, $apikey));
    $filters = array(
    array(
        'category_id' => 174
    ));
    

    并在重写类 Mage_Catalog_Model_Product_Api 方法项

    $collection = Mage::getModel('catalog/product')->getCollection()
                            ->addStoreFilter($this->_getStoreId($store))
                            ->addAttributeToSelect('name')
                            ->addAttributeToSelect('content_downloaded')
                        ;
        if (isset($_GET['order']) && isset($_GET['dir'])){
            $order = htmlspecialchars(strip_tags($_GET['order']));
            $dir = (strtoupper($_GET['dir']) == 'DESC' ) ? 'DESC' : 'ASC';
            $collection->setOrder($order, $dir);
        }
    
        if (isset($_GET['limit'])){
            $limit = intval($_GET['limit']);
            $collection->getSelect()->limit($limit);
        }
    

    它提供了 googd 测试。它与 HAKIM 建议的解决方案类似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 2012-05-24
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      相关资源
      最近更新 更多