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