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

How can I update product categories using prestashop web service?

您好,我想使用 prestashop 网络服务远程更新与产品相关的所有 "attributes"。几天来我一直在尝试更新它的类别,但没有成功。我正在使用 prestashop_1.6.1.5。 关注 doc 您可以获得这样的产品 xml

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

var_dump($xml);

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

那么如果你这样做

$resources->reference = "NEW REFERENCE";

你可以修改引用,例如。

可以通过

查看其分类
$resources->associations->categories->categories

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

$resources->associations->categories->categories[2] = 8

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

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

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

提前致谢

类别可以这样更新:

$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()));