Web Services Prestashop应用程序接口:编辑产品价格

iezvtpos  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(108)

使用: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>

它会传回:

<?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>

想不出有什么不对。

yh2wf1be

yh2wf1be1#

我已经发现,按引用搜索,如果将结果对象数据格式化为显示多个产品,我认为这样更新是不可用的。

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

在此结果之后,必须迭代结果ID。

相关问题