我尝试使用网络服务在两个prestashop网站之间同步一些产品。例如,同步后,来自目标网站的产品描述应与来自源网站的产品描述相同。
两个网站(源和目标(都是多语言的。源网站有英语和法语,目标网站有英语(id=4(、法语(id=5(和西班牙语(id=6(语言。
问题是目标网站的西班牙语现有描述在同步后被空白文本覆盖。其他字段也有同样的问题:简短描述、元描述、元标题。有趣的是link_rewrite
字段的文本保留为西班牙语。
以下是与网络服务一起发送的XML的一部分:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<product>
<id>8224</id>
<description>
<language id="4">English text</language>
<language id="5">Franch text</language>
</description>
</product>
</prestashop>
我想保留西班牙语的现有描述,即使我不在XML上发送文本。
最好的解决方案是检索现有对象,并且只更新您有兴趣更新的语言。
下面是仅使用一种语言更新产品名称的示例
<?php
include(__DIR__.'/config/config.inc.php');
include(__DIR__.'/init.php');
include(__DIR__.'/PSWebServiceLibrary.php');
/* Connect to the PrestaShop Web-service */
define('PS_SHOP_URL', 'http://localhost/prestashop');
define('PS_WS_AUTH_KEY', 'YOURKEYHERE');
$ws = new PrestaShopWebservice(PS_SHOP_URL, PS_WS_AUTH_KEY, false);
/* Retrieve the existing product (Product ID = 1) */
$xml = $ws->get(['url' => PS_SHOP_URL.'/api/products/1']);
echo 'Before: <pre>';
print_r($xml->product->name->language);
echo '</pre>';
/* Update only the name in French */
$xml->product->name->language[1] = 'New Name';
unset($xml->product->manufacturer_name, $xml->product->quantity, $xml->product->position_in_category); /* Removing fields which are not writeable */
$result = $ws->edit(['resource' => 'products', 'id' => 1, 'putXml' => $xml->asXML()]);
/* Check new values */
$xml = $ws->get(array('url' => PS_SHOP_URL.'/api/products/1'));
echo 'After: <pre>';
print_r($xml->product->name->language);
echo '</pre>';
结果/输出
Before:
SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[0] => Hummingbird printed t-shirt
[1] => Hummingbird printed t-shirt
)
After:
SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[0] => Hummingbird printed t-shirt
[1] => New Name
)
PSWebServiceLibrary.php文件可以在这里下载:https://github.com/PrestaShop/PrestaShop-webservice-lib/blob/master/PSWebServiceLibrary.php
您可以在此处找到使用PrestaShop网络服务的更多示例:
- 对于PrestaShop 1.6.x:http://doc.prestashop.com/display/PS16/Web+service+one-page+documentation
- 对于PrestaShop 1.7.x:https://devdocs.prestashop.com/1.7/development/webservice/tutorials/prestashop-webservice-lib/
我希望这有帮助!