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


How can I update product categories using prestashop web service?

嗨,我想使用 prestashop 网络服务远程更新与产品相关的所有"属性"。几天来我一直在尝试更新其类别,但没有成功。我正在使用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();

然后如果你这样做

$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不会出现在中间表中。我的意思是,如果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()));