Jump to content
jarek.skuder

Webservice add product along with the attributes and combinations

Recommended Posts

I am sending products from my DB to Prestashop DB via PrestaShopWebservice. The product is now sending and along with the product creates one attribute. The question is how to send product combinations or attributes? In Prestashop admin panel you can create combinations when you edit your product, it's easy. But how to do that via webservice?

I made this so far, i really thought that it should worked:

 

$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, true); // Starting webservice
$xml = $webService->get(array('resource' => 'products?schema=blank')); // Getting product schema

 

$resources = $xml->children()->children();
unset($resources->position_in_category);
unset($resources->manufacturer_name);
unset($resources->id_default_combination);
unset($resources->quantity);

$resources->id_category_default = '3';
$resources->id_shop_default = '1';
$resources->id_tax_rules_group = '1';
$resources->ean13 = $product->getEan13();
$resources->minimal_quantity = '1';
$resources->price = floatval($price['price']);
$resources->supplier_reference = $product->getMatNr();
$resources->active = '1';
$resources->available_for_order = '1';
$resources->show_price = '1';

$link = $this->remove_accent($prekesKortelesName);
$link = preg_replace('/[^a-zA-Z0-9]/', '_', $link);

$resources->link_rewrite->language[0][0] = $link;
$resources->link_rewrite->language[1][0] = $link;
$resources->link_rewrite->language[2][0] = $link;

$resources->name->language[0][0] = $prekesKortelesName;
$resources->name->language[1][0] = $prekesKortelesName;
$resources->name->language[2][0] = $prekesKortelesName;

$resources->description->language[0][0] = $product->getDescription();

$resources->id_tax_rules_group = '1';

 

$resources->associations->categories->category->id = $category->category->id;
$resources->id_category_default = $category->category->id;

$resources->associations->product_option_values->product_option_values->id = '1';
$resources->associations->product_features->product_feature->id = '1';
$resources->associations->product_features->product_feature->id_feature_value = '1';
$resources->associations->tags->tag->id = '1';
$resources->associations->stock_availables->stock_available->id = '1';
$resources->associations->stock_availables->stock_available->id_product_attribute = '905';
$resources->associations->accessories->product->id = '1';
$resources->associations->product_bundle->products->id = '1';

$opt['postXml'] = $xml->asXML();
$response = $webService->add($opt);

$resources = $response->children()->children();
$id_created_product = $resources->product->id;

$this->sendImage($product,$id_created_product);

// Product options

$xml = $webService->get(array('resource' => 'product_options?schema=blank'));

$resources->is_color_group = true;
$resources->group_type = 'color';

$resources->name->language[0][0] = $link;
$resources->name->language[1][0] = $link;
$resources->name->language[2][0] = $link;

$resources->public_name->language[0][0] = $prekesKortelesName;
$resources->public_name->language[1][0] = $prekesKortelesName;
$resources->public_name->language[2][0] = $prekesKortelesName;


$opt['postXml'] = $xml->asXML();
$response = $webService->add($opt);
$resources = $response->children()->children();
$id_attribute_group = $resources->id;

// Product option values

$xml = $webService->get(array('resource' => 'product_option_values?schema=blank'));

$resources->id_attribute_group = $id_attribute_group;

$resources->name->language[0][0] = $link;
$resources->name->language[1][0] = $link;
$resources->name->language[2][0] = $link;

$opt['postXml'] = $xml->asXML();
$response = $webService->add($opt);
$resources = $response->children()->children();
$id_product_option = $resources->id;

// Combinations

$xml = $webService->get(array('resource' => 'combinations?schema=blank'));

$resources->id_product = $id_created_product;
$resources->associations->product_option_values->product_option_values->id = $id_product_option;

$resources->minimal_quantity = '1';

$opt['postXml'] = $xml->asXML();
$response = $webService->add($opt);
$resources = $response->children()->children();

 

What i am missing???

Share this post


Link to post
Share on other sites

The main problem is that you assume the webservice API allows you to insert or update using associations. Except for a few badly documented cases it doesn't. So you need to split your post calls.

 

First write products (using associations here to assign categories is supported)

Then write combinations (using associations here to assign product option values is supported)

Then write stock_availables and any other things you need that refer back to products and combinations

Share this post


Link to post
Share on other sites

Sorry can you help me here. So as i understood corectlly, i must add product, categories, combinations, product_option_values and stock_availables?

Share this post


Link to post
Share on other sites

Yes, you need to add the different entities one by one using separate calls to the webservice add, you cannot group it all up in one call like you did in your settings of $resources->associations before writing the product.

 

I see I did one mistake in my previous reply, I didn't notice you created product_option_values as well.

 

So you then need to call the webservice add in this sequence:

 

product_option_values (then get back the IDs as you need them later)

products (you may set associations for categories. also get back the ID as you need it later)

images

combinations (you may set associations for product_option_values and images. also get back the IDs as you need them later)

stock_availables

 

This is all really badly documented by PrestaShop. I had to basically read their sourcecode and do a lot of trial and error to figure it all out.

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

 

Were you able to do this successfully? Are you running this with python or PHP? Can you give me a draft/example of what you came up with?

 

Thanks!

Edited by carolinalour (see edit history)

Share this post


Link to post
Share on other sites

Hi, could you please help? I've been trying to do this with python but the library isn't working. could you tell me how you did? Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More