Jump to content

[SOLVED] Webservices API problem: SORTING by PRICE returns no results in Prestashop 1.5.2


Recommended Posts

Hello, did anybody have this problem?

 

I'm trying to get the products list using Prestashop Webservices API but when I add sortingby price, the return xml is empty, it contains no products as if they disappear.

 

http://www.my_shop_address.com/api/products/?display=full&sort=[price_DESC]

 

From what I tested already sort returns no products for: price, position_in_category and several other important sortable attributes.

 

[sOLUTION]

 

A Question to the Presta Team

 

Hi Presta Team! If one of you guys/girls could spare a few seconds to explain me why it's impossible to simply sort Products by Price using MYSQL "ORDER BY" clause? I would greatly, greatly appreciate a short answer as it would help me a lot in understanding the shop. I'm a beginner PHP enthusiast and I'm totally backwards in learning MYSQL.

 

Second: The quick, temporal solution

 

Edit /overrides/classes/webservice/WebservicesRequest.php

and put this inside between the brackets:

 

 public function getFilteredObjectList()
 {
  $objects = array();
  $filters = $this->manageFilters();
  $this->resourceConfiguration['retrieveData']['params'][] = $filters['sql_join'];
  $this->resourceConfiguration['retrieveData']['params'][] = $filters['sql_filter'];
  $this->resourceConfiguration['retrieveData']['params'][] = $filters['sql_sort'];
  $this->resourceConfiguration['retrieveData']['params'][] = $filters['sql_limit'];
  //list entities
  $tmp = new $this->resourceConfiguration['retrieveData']['className']();
  $sqlObjects = call_user_func_array(array($tmp, $this->resourceConfiguration['retrieveData']['retrieveMethod']), $this->resourceConfiguration['retrieveData']['params']);
  if ($sqlObjects)
  {

   $comparo = array();
   /* THE CHANGE: */
   foreach ($sqlObjects as $sqlObject) {
 $newProdo = new $this->resourceConfiguration['retrieveData']['className']((int)$sqlObject[$this->resourceConfiguration['fields']['id']['sqlId']]);
 $objects[] = $newProdo;
 $comparo[] = (float)$newProdo->price;
   }

   if(!empty($this->urlFragments['baziprice'])) {
 if ($this->urlFragments['baziprice']=='DESC')
  array_multisort($comparo, SORT_DESC, $objects);
 else
  array_multisort($comparo, $objects);
   }


   return $objects;

  }
 }

 

Usage:

 

Instead of using:

 

http://www.your.page.com/api/products/?display=full&sort=[price_ASC]

 

Call this address (sort: ascending):

 

http://www.your.page.com/api/products/?display=full&baziprice=ASC

 

Or this (sort: descending):

 

http://www.your.page.com/api/products/?display=full&baziprice=DESC

Edited by adhd_kid (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

Hey,

 

I am pulling my hair out for sometime now .. I am using prestashop 1.5.2 and I am not able to use webservice. My problem is that the download does not contain an api folder under the shop. Every single tutorial seems to be refering to the api folder. I google up a lot and surprisingly no one seems to be missing the api folder...

 

Am I missing something ?

Link to comment
Share on other sites

×
×
  • Create New...