如何使用 prestashop 网络服务更新产品类别?

Adr*_*uez 1 php prestashop prestashop-1.6

您好,我想使用 prestashop Web 服务远程更新与产品相关的所有“属性”。我几天来一直试图更新其类别,但没有成功。我正在使用 prestashop_1.6.1.5。按照文档,您可以获得像这样的产品 xml

$xml = $this->webService->get(array('url' => 'http://prestashop.localhost/api/products/2'));

var_dump($xml);

$resources = $xml->children()->children();
Run Code Online (Sandbox Code Playgroud)

那么如果你这样做

$resources->reference = "NEW REFERENCE";
Run Code Online (Sandbox Code Playgroud)

例如,您可以修改参考。

可以通过以下方式查看其类别

$resources->associations->categories->categories
Run Code Online (Sandbox Code Playgroud)

您将获得与产品相关的类别 ID 数组。但如果你这样做:

$resources->associations->categories->categories[2] = 8
Run Code Online (Sandbox Code Playgroud)

您不会将与产品关联的第三个类别更新为 8。它将保持为 0。我还尝试为其分配一个字符串。我尝试取消设置整个类别节点,使用其使用的相同格式创建我自己的节点,然后再次对其进行分配。我还尝试创建一个 SimpleXMlElement 并使用 addChild() 为每个我想要修改的 id 添加它。但没有任何效果。

有人知道如何更新分类吗?

我还有另一个问题,这些类别 id 和产品 xml 中出现的 default_category_id 有什么区别?如果您看到 prestashop DDBB,default_category_id 不会出现在中间表中。我的意思是,如果 default_category_id 是 9,那么当您开始使用 prestashop 时,示例产品中的其他 id 是 2、3、4 和 7。

提前致谢

小智 5

类别可以这样更新:

$id_product = 102;
$new_product_categories = array(29,30,31); // List of categories to be linked to product

$xml = $this->webservice->get(array('resource' => 'products', 'id' => $id_product));

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

// Unset fields that may not be updated
unset($product->manufacturer_name);
unset($product->quantity);

// Remove current categories
unset($product->associations->categories); 

// Create new categories
$categories = $product->associations->addChild('categories'); 

foreach ($new_product_categories as $id_category) {
    $category = $categories->addChild('category');
    $category->addChild('id', $id_category);
}

$xml_response = $this->webservice->edit(array('resource' => 'products', 'id' => $id_product, 'putXml' => $xml->asXML()));
Run Code Online (Sandbox Code Playgroud)