chiken
chiken

Reputation: 31

Webservice Add/Update Combinations Prestashop

I read, i try, i looking informations about how add / update combinations by webservice, in presta 1.5.3 but still i don't know how to do that.

Can someone help me?

Upvotes: 3

Views: 7728

Answers (2)

Haroun
Haroun

Reputation: 21

This is work fine for me :

$webService = new PrestaShopWebservice($url, $api_key, FALSE);
$xml = $webService->get(array('url' => $url .'/api/combinations?schema=blank'));

$resources = $xml->children()->children();

$resources->id_product          = $ps_product_id;
$resources->wholesale_price     = $wholesale_price;
$resources->price               = $price;
$resources->unit_price_impact   = $unit_price_impact;
$resources->minimal_quantity    = $minimal_quantity;
$resources->quantity            = $quantity;
$resources->weight              = $weight;

$resources->associations->product_option_values->product_option_value[0]->id = $color_id;
$resources->associations->product_option_values->product_option_value[1]->id = $size_id;

$request = $xml->asXML();

//This is a function that curl request to specific URL using method (POST)
$response = ps_curl($url . '/api/combinations', $request, 'POST', $api_key);

$xml_load  = simplexml_load_string($response);
$id        = $xml_load->combination->id;

I hope that's helpful :)

Upvotes: 0

Tuco
Tuco

Reputation: 51

Assigning combinations to products via Webservice is a multi-step operation (unlike CSV import).

  • given a product with id_product
  • add product_options (BO Attribute Names)
  • add product_option_values (BO Attribute Values) to product_options
  • add combinations while specifying id_product

Start by initialising PrestaShopWebservice with DEBUG=true:

$api = new PrestaShopWebservice($psShopUrl, $psAuthKey, $psDebug);

Instead of building the XML from scratch get a template for the resource you need like this:

$sxml = $api->get(array('url' => $psShopUrl.'api/'.$resource.'?schema=blank'));

The response is a SimpleXMLElement which is a easier to manipulate than DOM.

NB: The response contains all wrapper nodes and you must send the same back in your request i.e. PSWebServiceLibrary will not recreate them for you.

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<combination>
...
</combination>
</prestashop>

SXML manipulation example:

$schema = $api->get(array('url' => $psShopUrl.'api/product_options?schema=blank'));
$data = $schema->children()->children();
$data->is_color_group = false;
$data->group_type = $group_type;   // radio, select
$data->name->language[0] = 'attribute private name';
$data->public_name->language[0] = 'attribute public name';
$xml = $schema->asXML();    // all of it!
$ret = $api->add(array('resource' => 'product_options', 'postXml' => $xml));
$id_attribute_group = (int)$ret->children()->children()->id;    // save for next step

Then get product_option_values schema, set data and the id_attribute_group from previous step. And so on.

Updating is the same except you will get the resource by id and then edit:

$sxml = $api->get(array('resource' => $resource, 'id' => $id));
...
$ret = $api->edit(array('resource' => $resource, 'id' => $id, 'putXml' => $xml));

As for adding multiple id values to the product_option_values node in the combinations resource you can use the array_push shortcut []:

$data->associations->product_option_values->product_option_values[]->id = 123;
$data->associations->product_option_values->product_option_values[]->id = 456;

Upvotes: 1

Related Questions