Reputation: 706
Maybe some of you who uses prestashop core classes know how to create product with custom attributes? I mean add new product in prestashop system by creating script that uses prestashop class for this purpose programatically.
Upvotes: 3
Views: 2942
Reputation: 69
His code working fine.if you create new attribute programmatically use below code to add a new attribute and get $idProductAttribute
use PrestaShop\PrestaShop\Adapter\Import\ImportDataFormatter;
$newGroup = new AttributeGroup();
$newGroup->name = ImportDataFormatter::createMultiLangField('test');
$newGroup->public_name = ImportDataFormatter::createMultiLangField('test');
$newGroup->group_type = 'select';
$newGroup->add();
$newAttribute = new Attribute();
$newAttribute->name = ImportDataFormatter::createMultiLangField('test');
$newAttribute->id_attribute_group = $newGroup->id;
$newAttribute->add();
$idProductAttribute = $product->addProductAttribute($price,
$weight,
$unit_impact,
$ecotax,
$quantity,
$id_images,
$reference,
$id_supplier,
$ean13,
$default,
$location,
$upc,
$minimal_quantity,
$isbn,
$low_stock_threshold,
$low_stock_alert);
$product->addAttributeCombinaison($idProductAttribute, array($newAttribute->id_attribute_group));
This is working for me.
Upvotes: 2
Reputation: 1395
I created products with attributes in PrestaShop 1.7.5, here a part of my code.
First load the product:
$product = new \Product($productId);
Then, add an attribute to this product:
// The $data array contains all of the required data for an attribute.
$idProductAttribute = $product->addProductAttribute(
(float)$data['price'], // Product price (+/-) - If the base product price is 50 and you set here -5, the new price will be 45
(float)$data['weight'], // Product weight
$data['unit_impact'],
$data['ecotax'],
(int)$data['quantity'], // Products available in stock
$data['id_images'], // Image ids
$data['reference'],
strval($data['suppliers']),
strval($data['ean13']), // Barcode
$data['default'], // Default product or not (1/0)
$data['location'],
$data['upc'],
$data['minimal_quantity'], // Default 1
$data['isbn']
);
$product->addAttributeCombinaison($idProductAttribute, $data['attribute_ids']); // $data['attribute_ids'] - id(s) of existing attribute(s).
And voilá, you have a product with a new attribute.
Upvotes: 1