Prestashop Webservice更新购物车产品'数量' PHP

时间:2014-07-21 14:25:16

标签: php api rest prestashop

我花了很多天与REST API购物车更新功能作斗争。它和地狱一样难,但它几乎不起作用。我无法用变量解决一个问题。 更新所有选项时如何输入项目数量?

我知道它只是几个代码符号,但不知道现在该做什么。

请帮帮我!

以下是我的功能:

<?php
session_start();
define('DEBUG', true);
define('PS_SHOP_PATH', 'http://mypresta-shop.com/');
define('PS_WS_AUTH_KEY', 'GAN3F82KAA71GZRHJF9QYED7UBTFUVF6');
require_once('/home/t/katja89/shop/public_html/PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);


function aCart($T_id, $id_customer, $id_address, $T_action){ //aCart(17, 3, 5, 'edit');

global $webService;

 if ($T_action == 'edit') { //  cart update, item is added, but the quantity does not update

$opt = array('resource' => 'carts');
$opt['id'] = $T_id; // 17
$xml = $webService->get($opt);
$resources = $xml->children()->children()->children();

unset($xml->children()->children()->associations->cart_rows->cart_row);

        foreach ($_SESSION["products"] as $cart_itm) { 

// I take the couple $cart_itm["code"] and $cart_itm["qty"], but cannot change 'quantity' of items added from the session :(

$xml->children()->children()->associations->cart_rows->addChild('cart_row')->addChild('id_product', $cart_itm["code"]);

//$xml->children()->children()->associations->cart_rows->addChild('cart_row')->addChild('quantity', $cart_itm["qty"]);
//$xml->children()->children()->associations->cart_rows->cart_row->quantity = $cart_itm["qty"];

       }

$opt = array('resource' => 'carts');
$opt['putXml'] = $xml->asXML();
$opt['id'] = $T_id; // 17
$xml = $webService->edit($opt);

} else if  ($T_action == 'add') { // adding a new cart works

$Tovar = "";

        foreach ($_SESSION["products"] as $cart_itm)
        {
$Tovar .= "<cart_row>";
$Tovar .= "<id_product>".$cart_itm["code"]."</id_product>";
$Tovar .= "<id_product_attribute>0</id_product_attribute>";
$Tovar .= "<quantity>".$cart_itm["qty"]."</quantity>";
$Tovar .= "</cart_row>";
        }

$Tovar .= "";

//$id_address = "5";
//$id_customer = "3";

$psXML = <<<XML
<prestashop>
<cart>
  <id/>
  <id_address_delivery>{$id_address}</id_address_delivery>
  <id_address_invoice>{$id_address}</id_address_invoice>
  <id_currency>1</id_currency>
  <id_customer>{$id_customer}</id_customer>
  <id_guest>0</id_guest>
  <id_lang>1</id_lang>
  <id_carrier>0</id_carrier>
  <recyclable>0</recyclable>
  <gift>0</gift>
  <gift_message/>
  <associations>
   <cart_rows>
    {$Tovar}
   </cart_rows>
  </associations>
</cart>
</prestashop>
XML;


$xml = new SimpleXMLElement($psXML);
$opt = array( 'resource' => 'carts' );
$opt['postXml'] = $xml->asXML();
$xml = $webService->add( $opt );
}
}

我没有特别的错误。 在我的会话中,我持有产品ID(购物车商品代码 - $ cart_itm [“代码”])和数量(购物车商品数量$ cart_itm [“qty”])。当用户更改这些变量时,我需要更改购物车内容。为此,我打电话给aCart(17,3,5,'编辑');功能。如果我添加新购物车aCart(17,3,5,'添加'),它可以正常工作;但我不能编辑 cart_rows。在cart_row数量中,$ cart_itm [“qty”]与id_product连接为$ cart_itm [“code”]。我无法将特定商品的$ cart_itm [“qty”]插入cart_row。

在代码中,在foreach行下,您会看到我尝试的两个选项(灰色),但都不起作用。我需要获得购物车中每件商品的更新数量。

$xml->children()->children()->associations->cart_rows->addChild('cart_row')->addChild('id_product', $cart_itm["code"]);

效果很好,它会添加带项目ID的cart_row。但它显示数量为0.我不知道如何让它向我显示id_product的真实数量($ cart_itm [“qty”])($ cart_itm [“code”])。

1 个答案:

答案 0 :(得分:0)

几年后......

你可以这样做:

x