skip to Main Content

I have rewrited Api model of Magento Core (M1.9) and want to test how it works. It returns too may records and I want to set conditions like in Sql “order BY Desc|Asc” and “LIMIT” in my tests. But i dont know where should i place mentioned conditions.
Here is my code for tests:

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

I will be appreciated for any advice

3

Answers


  1. Chosen as BEST ANSWER

    I tried to transfer the parameters limit,dir, order into url request by GET method like:

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

    And in rewrited class Mage_Catalog_Model_Product_Api method item

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

    And it gave googd tests.It is similar Solution as HAKIM suggested.


  2. Please, try to test the code below:

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

    I haven’t tested it by myself. I took the format of filter parameters from the link http://devdocs.magento.com/guides/m1x/api/rest/get_filters.html
    Maybe it will work for your case, too.

    Login or Signup to reply.
  3. You have to rewrite this class and override items function
    class = Mage_Catalog_Model_Product_Api
    function = items

      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"]);
            }
    

    then you can call it by

    $filters = []; 
    $extra = ["cur_page"=>1,"page_size"=>"3","order"=>["field"=>"name", "type"=>"desc"]];
    
    $result= $proxy->call($sessionId, 'catalog_product.list',[$filters,null,$extra]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search