3

我读过,我尝试过,我在 presta 1.5.3 中查看有关如何通过 web 服务添加/更新组合的信息,但我仍然不知道该怎么做。

有人能帮我吗?

4

2 回答 2

1

通过 Web 服务将组合分配给产品是一个多步骤操作(与 CSV 导入不同)。

  • 给定一个带有 id_product 的产品
  • 添加 product_options(BO属性名称
  • 将 product_option_values(BO属性值)添加到 product_options
  • 在指定 id_product 时添加组合

首先使用 DEBUG=true 初始化 PrestaShopWebservice:

$api = new PrestaShopWebservice($psShopUrl, $psAuthKey, $psDebug);

无需从头开始构建 XML,而是为您需要的资源获取模板,如下所示:

$sxml = $api->get(array('url' => $psShopUrl.'api/'.$resource.'?schema=blank'));

响应是一个 SimpleXMLElement,它比 DOM 更容易操作。

注意:响应包含所有包装节点,您必须在您的请求中发送相同的内容,即 PSWebServiceLibrary不会为您重新创建它们。

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

SXML 操作示例:

$schema = $api->get(array('url' => $psShopUrl.'api/product_options?schema=blank'));
$data = $schema->children()->children();
$data->is_color_group = false;
$data->group_type = $group_type;   // radio, select
$data->name->language[0] = 'attribute private name';
$data->public_name->language[0] = 'attribute public name';
$xml = $schema->asXML();    // all of it!
$ret = $api->add(array('resource' => 'product_options', 'postXml' => $xml));
$id_attribute_group = (int)$ret->children()->children()->id;    // save for next step

然后获取product_option_values模式,设置数据和id_attribute_group上一步的数据。等等。

更新是相同的,除了您将get通过id获取资源,然后edit

$sxml = $api->get(array('resource' => $resource, 'id' => $id));
...
$ret = $api->edit(array('resource' => $resource, 'id' => $id, 'putXml' => $xml));

至于向资源中的节点添加多个id值,您可以使用 array_push 快捷方式:product_option_valuescombinations[]

$data->associations->product_option_values->product_option_values[]->id = 123;
$data->associations->product_option_values->product_option_values[]->id = 456;
于 2014-11-03T16:36:09.423 回答
0

这对我来说很好:

$webService = new PrestaShopWebservice($url, $api_key, FALSE);
$xml = $webService->get(array('url' => $url .'/api/combinations?schema=blank'));

$resources = $xml->children()->children();

$resources->id_product          = $ps_product_id;
$resources->wholesale_price     = $wholesale_price;
$resources->price               = $price;
$resources->unit_price_impact   = $unit_price_impact;
$resources->minimal_quantity    = $minimal_quantity;
$resources->quantity            = $quantity;
$resources->weight              = $weight;

$resources->associations->product_option_values->product_option_value[0]->id = $color_id;
$resources->associations->product_option_values->product_option_value[1]->id = $size_id;

$request = $xml->asXML();

//This is a function that curl request to specific URL using method (POST)
$response = ps_curl($url . '/api/combinations', $request, 'POST', $api_key);

$xml_load  = simplexml_load_string($response);
$id        = $xml_load->combination->id;

我希望这会有所帮助:)

于 2015-09-16T08:52:46.697 回答