John Davidson

php - Prestashop - Add multiple categories to a product

0 comments
Message:


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 :



Thanks in advance!



My Answer:
>id);foreach($xml->categories->category as $category){$product->addToCategories(array('id_category' => (int)$category->id));}$product->save();echo "Categories added successfully";?>

This code should add multiple categories to a product in Prestashop. Make sure to replace the placeholder values with your actual data.

Rate this post

4 of 5 based on 6111 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us