web服务-获取当前购物车ID在预览版



我正忙于一个建立在Code Igniter上的网站,需要与prestshop集成。在站点中,当创建用户帐户时,我将"shop_id"保存到数据库中,然后在登录时将其作为会话变量检索。

我正在使用prestshop API成功检索客户(使用上面的"shop_id")

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

这将成功返回所讨论的用户,但是在此结果中没有Cart id。

登录到我的商店后端,我看到有多个购物车与登录的用户相关联。

我的问题是:如何使用API检索最新购物车ID ?

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

有点晚了,但我遇到了同样的问题,并根据yenshirak的提示找到了一种方法来完成查询(因为购物车id是降序的,第一个发现的将是最新的)。

我直接使用邮差查询API,像这样:

获取所有购物车:

GET webserver/api/carts

get cart for customer:

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

获取最新的:

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

为美观的打印,您还可以添加参数:

display=full&output_format=JSON

你可以在php中这样做,我没有测试语法是否正确,但根据文档,它看起来像这样:

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

最新更新