简体   繁体   English

Prestashop - 为产品添加多个类别

[英]Prestashop - Add multiple categories to a product

I'm trying to add more categories in my product but once created, the product keeps only the id_cetegory_default.我正在尝试在我的产品中添加更多类别,但一旦创建,产品仅保留 id_cetegory_default。

My code :我的代码:

$xml = simplexml_load_file('php://input');
$product = new Product($xml->id);
$product->category = [2,7];
$product->id_category_default = (int)$xml->id_category_default; //categories
$product->name = $xml->name; //nom produit
$product->price = $xml->price; //prix
$product->id_tax_rules_group = (int)$xml->id_tax_rules_group;
$product->ean13 = $xml->ean13; //code barre
$product->description = $xml->description; //description
$product->description_short = $xml->description_short; //petite description
$product->reference = $xml->reference; //reference
$product->weight = $xml->weight; //poids
$product->height = $xml->height; //hauteur
$product->width = $xml->width; //largeur
$product->depth = $xml->depth; //profondeur
$product->indexed = 1;
$product->active = true;
$product->save();
$e = $product->getDefaultIdProductAttribute();
StockAvailable::setQuantity($product->id, $e, $xml->quantity);
$image = new Image();
$image->id_product = $product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = true;
$image->add();
AdminImportControllerCore::copyImg((int)$product->id, (int)$image->id, $xml->urlImage, 'products', false);

Result :结果 :

在此处输入图像描述

Thanks in advance!提前致谢!

You need to call updateCategories function to add product into multiple categories.您需要调用updateCategories函数将产品添加到多个类别中。 So once your product get saved;因此,一旦您的产品得到保存; you can call this funtion.你可以调用这个函数。

For example:例如:

$product->updateCategories($array_of_category_ids_here);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM