2017-11-10 197 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提出類似的解決方案。