Home > Back-end >  Prestashop - Add multiple categories to a product
Prestashop - Add multiple categories to a product

Time:06-11

I'm trying to add more categories in my product but once created, the product keeps only the 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 :

enter image description here

Thanks in advance!

CodePudding user response:

You need to call updateCategories function to add product into multiple categories. So once your product get saved; you can call this funtion.

For example:

$product->updateCategories($array_of_category_ids_here);
  • Related