Prestashop API:编辑产品价格

Prestashop API: edit product price

使用:Prestashop:1.7.6.5.

我正在尝试使用以下代码创建一段代码来更新产品价格(或产品中的其他信息),但它总是导致错误“修改资源时需要 id”。

代码:

<?
require_once 'PSWebServiceLibrary.php';

$url = 'myurl.com';
$key = 'ABCDEFGHIJKLMNOPQRSTUVXZ1234567890';

$debug = true;
$searchTerm = "ZXCVBNM";

try {
    $webService = new PrestaShopWebservice($url, $key, $debug);
    $xml = $webService->get([
        'resource' => 'products',
        'display' => 'full',
        'filter[reference]' => $searchTerm
    ]);
} catch (PrestaShopWebserviceException $ex) {
    echo 'Other error: <br />' . $ex->getMessage();
}

$productFields = $xml->products->children()->children();

unset($productFields->manufacturer_name);
unset($productFields->quantity);
unset($productFields->id_shop_default);
unset($productFields->id_default_image);
unset($productFields->associations);
unset($productFields->id_default_combination);
unset($productFields->position_in_category);
unset($productFields->type);
unset($productFields->pack_stock_type);
unset($productFields->date_add);
unset($productFields->date_upd);

$productFields->price = (int) "666.00";

$updatedXml = $webService->edit([
    'resource' => 'products',
    'id' => (int) $productFields->id,
    'putXml' => $xml->asXML(),
]);

GET 结果为:

RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
    <id><![CDATA[21346]]></id>
    ...
</product>
</products>
</prestashop>

PUT:

XML SENT

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
    <id>21346</id>
    ... 
    <price>666</price>
    ...
</product>
</products>
</prestashop>

它returns:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[90]]></code>
<message><![CDATA[id is required when modifying a resource]]></message>
</error>
</errors>
</prestashop>

不知道哪里出了问题。

我发现通过引用搜索,结果对象数据如果格式化为显示多个产品,我认为以这种方式更新它不可用。

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
...
<product>
<products>

在得到这个结果之后,必须遍历结果 ID。