Question

I am busy with a site built on Code Igniter that needs integration with Prestashop. In the site, when creating a user account, I save a "shop_id" to the database, which is then retrieved as a session variable when logging in.

I am using the Prestashop API to retrieve the customer successfully (using above "shop_id")

$xml = $this->PSWebService->get(
    array('resource' => 'customers', 'id' => (int)$this->user['shop_id'])
);

This successfully returns the user in question, but there is no Cart IDs in this result.

Logging in to the back-end of my shop, I see that there are multiple carts associated with the logged in user.

My question is: How to I retrieve the LATEST cart ID using the API?

Was it helpful?

Solution

$userId = (int) $this->user['shop_id'];
$opt['resource'] = 'carts';
$xml = $this->PSWebService->get($opt);
$carts = $xml->carts->children();
foreach ($carts as $cart) {
    $cartIds[] = $cart['id'];
}
for ($i = count($cartIds) - 1; $i > -1; $i--) {
  $opt['id'] = $cartIds[$i];
  $xml = $this->PSWebService->get($opt);
  //since cart ids are descending the first found will be the latest
  if ($xml->cart->id_customer == $userId) {
    $latestCartId = $cartIds[$i];
    break;
  }
}

OTHER TIPS

Kind of late, but I struggled a bit with this same problem and found a way to do it just from query, based on yenshirak's tip (since cart ids are descending the first found will be the latest).

I query the API directly using postman like this:

get all carts:

GET webserver/api/carts

get cart for customer:

GET webserver/api/carts?filter[id_customer]=1

get the most recent:

GET webserver/api/carts?filter[id_customer]=1&sort=[id_DESC]&limit=1

for a pretty print, you can also add params:

display=full&output_format=JSON

You can do this in php, I have not tested if the syntax is correct, but based on documentation it looks something like this:

$opt = array( 'resource' => 'carts', 'filter[id_customer]' => '[1]', 'sort' => '[id_DESC]', 'limit' => '1' ); $xml = $webService->get($opt);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top