nikksan
nikksan

Reputation: 3481

Prestashop Add Product Combination

I have created a small script to add products to a prestashop store. Now I am facing an issue - trying to add combination to a product. Here is how my code looks like now. After I add the product here is what I`m doing , $product_id is my newly created product

$xml = $webService->get(array('url' => SITE_URL.'/api/combinations?schema=blank'));
$combinations = $xml->children()->children();
$combinations->id_product = $product_id;

$combinations->minimal_quantity = 1;
$combinations->reference = 'dada';
$combinations->price = 99;
$combinations->default_on = 1;
$combinations->associations->product_option_values->product_option_value->id = 1;
$opt = array('resource' => 'products');
$opt['postXml'] = $xml->asXML();

and this is the response from the server ..

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<combination>
<id></id>
<id_product></id_product>
<location></location>
<ean13></ean13>
<upc></upc>
<quantity></quantity>
<reference></reference>
<supplier_reference></supplier_reference>
<wholesale_price></wholesale_price>
<price></price>
<ecotax></ecotax>
<weight></weight>
<unit_price_impact></unit_price_impact>
<minimal_quantity></minimal_quantity>
<default_on></default_on>
<available_date></available_date>
<associations>
<product_option_values>
<product_option_value>
<id></id>
</product_option_value>
</product_option_values>
<images>
<image>
<id></id>
</image>
</images>
</associations>
</combination>
</prestashop>

Upvotes: 1

Views: 2974

Answers (2)

nikksan
nikksan

Reputation: 3481

I found a working code , it doesnt use the webservice.

$product = new Product( $product_id );
$combinationAttributes = array();
$combinationAttributes[] = $combination_id;   
$idProductAttribute = $product->addProductAttribute(
                (float)1, //price 
                (float)1, //weight
                1,        //unit_impact
                null ,    //ecotax
                (int)1,   //quantity
                "",       //id_images
                "" , //reference
                strval(""), //suppliers
                strval(""), //ean13
                NULL, //default 
                NULL,  //location
                NULL  //upc
                );
                $product->addAttributeCombinaison($idProductAttribute,                     $combinationAttributes);

Upvotes: 0

Knowband Plugins
Knowband Plugins

Reputation: 1317

Try using the following code to add product combinations:

$product = new Product($id_product);

$id_product_attribute = $product->addCombinationEntity(
                                Tools::getValue('attribute_wholesale_price'),
                                Tools::getValue('attribute_price') * Tools::getValue('attribute_price_impact'),
                                Tools::getValue('attribute_weight') * Tools::getValue('attribute_weight_impact'),
                                Tools::getValue('attribute_unity') * Tools::getValue('attribute_unit_impact'),
                                Tools::getValue('attribute_ecotax'),
                                0,
                                Tools::getValue('id_image_attr'),
                                Tools::getValue('attribute_reference'),
                                null,
                                Tools::getValue('attribute_ean13'),
                                Tools::getValue('attribute_default'),
                                Tools::getValue('attribute_location'),
                                Tools::getValue('attribute_upc'),
                                Tools::getValue('attribute_minimal_quantity'),
                                array(),
                                Tools::getValue('available_date_attribute')
                            );

For more details you can check the definition of addCombinationEntity() function in Product.php class file.

The above function returns a generated combination_id.

Upvotes: 2

Related Questions