2017-11-10 188 views
2

我重写了Magento Core(M1.9)的Api模型,并且想测试它的工作原理。它也返回可能的记录,我想在我的测试中设置条件,如Sql“order BY Desc | Asc”和“LIMIT”。但我不知道我应该在哪里提到的条件。 这里是我的测试代码:SOAP API中的ASC和DESC条件magento 1.9

$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(); 
} 

我可以理解任何意见

回答

0

请尝试测试代码如下:

$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 采取了过滤器参数的格式也许它也适用于你的情况。

+0

谢谢你Aleks。我之前尝试过这种方式,但这并不成功。 –

0

你必须重写这个类并覆盖项目运作 类= 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]); 
+0

谢谢HAKIM的回答,我没有尝试你的解决方案,但我认为它会工作。我解决了我的问题,正如我在答案中所描述的那样,并且与您给出的方式相似。知道你对我的看法很有意思。 –

0

我试图转移参数的限制调用它, dir,通过GET方法命令进入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 
)); 

而且在rewrited类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 tests.It是HAKIM提出类似的解决方案。