ps_eln_apprentice Posted October 30, 2012 Share Posted October 30, 2012 Hello to the forum people. I have made some coding experiment with Webservices of Prestashop. I have incurred in two bugs of the Product Webservices. If PHP + WebService CREATE a new Product using the postXML/add , in the MySQL table ps_category_product two associations are created when only one is desired. id_category id product position 0 12 0 9 12 1 The association desired is 9 12 0. It is created also a spurious association with id_category 0 that has no meaning (home is 2). This seems only a nuissance but I don't know if in the future will became an issue. If PHP + WebService UPDATE a product using the putXML/edit, using "$resources->associations->categories->addChild('category')->addChild('id', $n_id_category);" I can add the same product to other category. ps_category_product table id_category id_product position 9 12 0 7 12 1 The product 12 is visible in two different category (7 and 9). The BUG is the following. If I try to add an already existing category to the product, all the category are deleted! The product is no longer owned by any category. The previous two row are deleted from ps_category_product table. This happens even if the product is created from backoffice and not by webservice. $n_id_category_default seems only used in display the main category of the product in the backoffice; it has no influence on frontoffice. Thanks in advance of the aid and the time. Follow the used code (are two different PHP files). Creation of product Adapted Code from a category example http://www.prestasho...via-webservice/ <?php define('DEBUG', true); define('PS_SHOP_PATH', 'http://localhost/prestashop1520'); // Root path of your PrestaShop store define('PS_WS_AUTH_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxx'); // Auth key (Get it in your Back Office) require_once ('./PSWebServiceLibrary.php'); $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); function PS_new_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later) { global $webService; $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/products?schema=blank')); $resources = $xml -> children() -> children(); unset($resources -> id); unset($resources -> position); unset($resources -> id_shop_default); unset($resources -> date_add); unset($resources -> date_upd); unset($resources->associations->combinations); unset($resources->associations->product_options_values); unset($resources->associations->product_features); unset($resources->associations->stock_availables->stock_available->id_product_attribute); //unset($resources->associations->categories->category->id); //unset($resources-> id_category_default); //$resources->position_in_category = '0'; //unset($resources->position_in_category); //$resources -> position = '0'; //$resources -> id; $resources-> id_manufacturer = '1'; $resources-> id_supplier = '1'; $resources-> id_category_default = $n_id_category_default; $resources-> new = '0'; ; //condition, new is also a php keyword!! $resources-> cache_default_attribute; $resources-> id_default_image; $resources-> id_default_combination = '0'; $resources-> id_tax_rules_group ='1'; //$resources-> id_shop_default; $resources-> reference; $resources-> supplier_reference; $resources-> location; $resources-> width; $resources-> height; $resources-> depth; $resources-> weight; $resources-> quantity_discount; $resources-> ean13; $resources-> upc; $resources-> cache_is_pack; $resources-> cache_has_attachments; $resources-> is_virtual; $resources-> on_sale; $resources-> online_only; $resources-> ecotax; $resources-> minimal_quantity = 1; $resources-> price = $n_price; $resources-> wholesale_price; $resources-> unity; $resources-> unit_price_ratio; $resources-> additional_shipping_cost; $resources-> customizable; $resources-> text_fields; $resources-> uploadable_files; $resources-> active = $n_active; $resources-> available_for_order = $n_avail4order; $resources-> available_date; $resources-> condition; $resources-> show_price = $n_show_price; $resources-> indexed = '1'; $resources-> visibility = 'both'; $resources-> advanced_stock_management='0'; $resources-> date_add; $resources-> date_upd; $resources->associations->categories->addChild('category')->addChild('id', $n_id_category); $node = dom_import_simplexml($resources -> name -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; $resources -> name -> language[0][0]['id'] = $n_l_id; $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; $resources -> description -> language[0][0]['id'] = $n_l_id; $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> description_short -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc_short)); $resources -> description_short -> language[0][0] = $n_desc_short; $resources -> description_short -> language[0][0]['id'] = $n_l_id; $resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_title -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; $resources -> meta_title -> language[0][0]['id'] = $n_l_id; $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_description)); $resources -> meta_description -> language[0][0] = $n_meta_description; $resources -> meta_description -> language[0][0]['id'] = $n_l_id; $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> available_now -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_available_now)); $resources -> available_now -> language[0][0] = $n_available_now; $resources -> available_now -> language[0][0]['id'] = $n_l_id; $resources -> available_now -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> available_later -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_available_later)); $resources -> available_later -> language[0][0] = $n_available_later; $resources -> available_later -> language[0][0]['id'] = $n_l_id; $resources -> available_later -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; try { $opt = array('resource' => 'products'); $opt['postXml'] = $xml -> asXML(); $xml = $webService -> add($opt); } catch (PrestaShopWebserviceException $ex) { echo '<b>Error : '.$ex->getMessage().'</b>'; // my log function } } // simple use $n_name = 'New product name3'; $n_desc = 'New product description LONG'; $n_desc_short = 'New product description SHORT'; $n_link_rewrite = 'someone_rewrite'; $n_meta_title = 'meta-title'; $n_meta_description = 'meta desc'; $n_meta_keywords = 'some,one,keywords'; $n_available_now = 'Available for order'; $n_available_later = 'Available from gg/mm/yy'; $n_id_category_default = '9'; $n_id_category = '9'; $n_price = '144.65'; $n_active = '1'; $n_avail4order = '1'; $n_show_price ='1'; $n_l_id = '1'; $n_id_stock_availables = '22'; $n_id_id_product_attribute = '0'; // run PS_new_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later); ?> UPDATE OF PRODUCT <?php define('DEBUG', true); define('PS_SHOP_PATH', 'http://localhost/prestashop1520'); // Root path of your PrestaShop store define('PS_WS_AUTH_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxx'); // Auth key (Get it in your Back Office) require_once ('./PSWebServiceLibrary.php'); $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); function PS_update_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later) { global $webService; try { $opt2 = array('resource' => 'products', 'id'=>'12'); $xml2 = $webService->get($opt2); $resources = $xml2->children()->children(); echo '<br />'; echo 'opt2'.'<br />'; var_dump ($opt2); echo '<br />'; echo '<br />'; echo '<br />'; echo 'xml2'.'<br />'; var_dump ($xml2); echo '<br />'; echo '<br />'; } catch (PrestaShopWebserviceException $e) { // Here we are dealing with errors echo '<b>Error : '.$e->getMessage().'</b>'; } /* unset($resources->position_in_category); unset($resources->manufacturer_name); unset($resources->quantity); //name and link_rewrite must be filled */ unset($resources->position_in_category); unset($resources->manufacturer_name); unset($resources->quantity); //unset($resources -> id); //unset($resources -> position); //unset($resources -> id_shop_default); //unset($resources -> date_add); //unset($resources -> date_upd); //unset($resources->associations->combinations); //unset($resources->associations->product_options_values); //unset($resources->associations->product_features); //unset($resources->associations->stock_availables->stock_available->id_product_attribute); //unset($resources->associations->categories->category->id); //unset($resources-> id_category_default); //$resources->position_in_category = '0'; //unset($resources->position_in_category); //$resources -> position = '0'; //$resources -> id; $resources-> id_manufacturer = '2'; $resources-> id_supplier = '2'; $resources-> id_category_default = $n_id_category_default; $resources-> new = '0'; //condition, new is also a php keyword!! $resources-> cache_default_attribute; $resources-> id_default_image; $resources-> id_default_combination = '0'; $resources-> id_tax_rules_group ='0'; $resources-> id_shop_default; $resources-> reference; $resources-> supplier_reference; $resources-> location; $resources-> width; $resources-> height; $resources-> depth; $resources-> weight; $resources-> quantity_discount; $resources-> ean13; $resources-> upc; $resources-> cache_is_pack; $resources-> cache_has_attachments; $resources-> is_virtual; $resources-> on_sale; $resources-> online_only; $resources-> ecotax; $resources-> minimal_quantity = 1; $resources-> price = $n_price; $resources-> wholesale_price; $resources-> unity; $resources-> unit_price_ratio; $resources-> additional_shipping_cost; $resources-> customizable; $resources-> text_fields; $resources-> uploadable_files; $resources-> active = $n_active; $resources-> available_for_order = $n_avail4order; $resources-> available_date; $resources-> condition; $resources-> show_price = $n_show_price; $resources-> indexed = '1'; $resources-> visibility = 'both'; $resources-> advanced_stock_management='0'; $resources-> date_add; $resources-> date_upd; //$node = dom_import_simplexml($resources -> name -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; //$resources -> name -> language[1][0] = $n_name.'L2'; //$resources -> name -> language[2][0] = $n_name.'L3'; //$resources -> name -> language[0][0]['id'] = $n_l_id; //$resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> description -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; //$resources -> description -> language[0][0]['id'] = $n_l_id; //$resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> description_short -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_desc_short)); $resources -> description_short -> language[0][0] = $n_desc_short; //$resources -> description_short -> language[0][0]['id'] = $n_l_id; //$resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; //$resources -> link_rewrite -> language[1][0] = $n_link_rewrite.'L2'; //$resources -> link_rewrite -> language[2][0] = $n_link_rewrite.'L3'; //$resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; //$resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_title -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; //$resources -> meta_title -> language[0][0]['id'] = $n_l_id; //$resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_description -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_description)); $resources -> meta_description -> language[0][0] = $n_meta_description; //$resources -> meta_description -> language[0][0]['id'] = $n_l_id; //$resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; //$resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; //$resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> available_now -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_available_now)); $resources -> available_now -> language[0][0] = $n_available_now; //$resources -> available_now -> language[0][0]['id'] = $n_l_id; //$resources -> available_now -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> available_later -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_available_later)); $resources -> available_later -> language[0][0] = $n_available_later; //$resources -> available_later -> language[0][0]['id'] = $n_l_id; //$resources -> available_later -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; try { $opt2 = array('resource' => 'products', 'id'=>'12'); echo '<br />'; echo 'opt2A'.'<br />'; var_dump ($opt2); echo '<br />'; echo '<br />'; $opt2['putXml'] = ($xml2->saveXML()); echo '<br />'; echo 'opt2B'.'<br />'; var_dump ($opt2); echo '<br />'; echo 'xml2'.'<br />'; var_dump ($xml2); echo '<br />'; echo '<br />'; echo 'xml2asxml'.'<br />'; var_dump ($xml2->asXML()); echo '<br />'; echo '<br />'; $xml3 = $webService->edit($opt2); //($opt); echo '<br />'; echo '<br />'; echo 'xml4'.'<br />'; var_dump ($xml3); echo '<br />'; echo '<br />'; // if WebService don't throw an exception the action worked well and we don't show the following message echo "Successfully updated."; } catch (PrestaShopWebserviceException $ex) { // Here we are dealing with errors echo '<b>Error : '.$ex->getMessage().'</b>'; } // simple use $n_name = 'New product 5'; $n_desc = 'New product description LONGrewr2'; $n_desc_short = 'New product description SHORTrewr2'; $n_link_rewrite = 'someone_rewriterewr2'; $n_meta_title = 'meta-titlerewr2'; $n_meta_description = 'meta descrewr2'; $n_meta_keywords = 'some,one,keywords,rewr2'; $n_available_now = 'Available for orderrewr2'; $n_available_later = 'Available from gg/mm/yyrewr2'; $n_id_category_default = '9'; $n_id_category = '7'; $n_price = '250.65'; $n_active = '1'; $n_avail4order = '1'; $n_show_price ='1'; $n_l_id = '1'; $n_id_stock_availables = '22'; $n_id_id_product_attribute = '0'; // run PS_update_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later); ?> Link to comment Share on other sites More sharing options...
dandumit Posted November 5, 2012 Share Posted November 5, 2012 Thank you for this informative example. I am trying to build a script to add/update products on my site. Would you please provide an example how to add a picture to an existing product ? Kind Regards, Daniel Link to comment Share on other sites More sharing options...
me.yasin Posted May 18, 2013 Share Posted May 18, 2013 @ps_eln_apprentice how to add product with multiple attributes like images, color, size etc with web services. Link to comment Share on other sites More sharing options...
LuisPL Posted June 19, 2013 Share Posted June 19, 2013 Hello to the forum people. I have made some coding experiment with Webservices of Prestashop. I have incurred in two bugs of the Product Webservices. If PHP + WebService CREATE a new Product using the postXML/add , in the MySQL table ps_category_product two associations are created when only one is desired. id_category id product position 0 12 0 9 12 1 The association desired is 9 12 0. It is created also a spurious association with id_category 0 that has no meaning (home is 2). This seems only a nuissance but I don't know if in the future will became an issue. If PHP + WebService UPDATE a product using the putXML/edit, using "$resources->associations->categories->addChild('category')->addChild('id', $n_id_category);" I can add the same product to other category. ps_category_product table id_category id_product position 9 12 0 7 12 1 The product 12 is visible in two different category (7 and 9). The BUG is the following. If I try to add an already existing category to the product, all the category are deleted! The product is no longer owned by any category. The previous two row are deleted from ps_category_product table. This happens even if the product is created from backoffice and not by webservice. $n_id_category_default seems only used in display the main category of the product in the backoffice; it has no influence on frontoffice. Thanks in advance of the aid and the time. Follow the used code (are two different PHP files). Creation of product Adapted Code from a category example http://www.prestasho...via-webservice/ <?php define('DEBUG', true); define('PS_SHOP_PATH', 'http://localhost/prestashop1520'); // Root path of your PrestaShop store define('PS_WS_AUTH_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxx'); // Auth key (Get it in your Back Office) require_once ('./PSWebServiceLibrary.php'); $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); function PS_new_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later) { global $webService; $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/products?schema=blank')); $resources = $xml -> children() -> children(); unset($resources -> id); unset($resources -> position); unset($resources -> id_shop_default); unset($resources -> date_add); unset($resources -> date_upd); unset($resources->associations->combinations); unset($resources->associations->product_options_values); unset($resources->associations->product_features); unset($resources->associations->stock_availables->stock_available->id_product_attribute); //unset($resources->associations->categories->category->id); //unset($resources-> id_category_default); //$resources->position_in_category = '0'; //unset($resources->position_in_category); //$resources -> position = '0'; //$resources -> id; $resources-> id_manufacturer = '1'; $resources-> id_supplier = '1'; $resources-> id_category_default = $n_id_category_default; $resources-> new = '0'; ; //condition, new is also a php keyword!! $resources-> cache_default_attribute; $resources-> id_default_image; $resources-> id_default_combination = '0'; $resources-> id_tax_rules_group ='1'; //$resources-> id_shop_default; $resources-> reference; $resources-> supplier_reference; $resources-> location; $resources-> width; $resources-> height; $resources-> depth; $resources-> weight; $resources-> quantity_discount; $resources-> ean13; $resources-> upc; $resources-> cache_is_pack; $resources-> cache_has_attachments; $resources-> is_virtual; $resources-> on_sale; $resources-> online_only; $resources-> ecotax; $resources-> minimal_quantity = 1; $resources-> price = $n_price; $resources-> wholesale_price; $resources-> unity; $resources-> unit_price_ratio; $resources-> additional_shipping_cost; $resources-> customizable; $resources-> text_fields; $resources-> uploadable_files; $resources-> active = $n_active; $resources-> available_for_order = $n_avail4order; $resources-> available_date; $resources-> condition; $resources-> show_price = $n_show_price; $resources-> indexed = '1'; $resources-> visibility = 'both'; $resources-> advanced_stock_management='0'; $resources-> date_add; $resources-> date_upd; $resources->associations->categories->addChild('category')->addChild('id', $n_id_category); $node = dom_import_simplexml($resources -> name -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; $resources -> name -> language[0][0]['id'] = $n_l_id; $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; $resources -> description -> language[0][0]['id'] = $n_l_id; $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> description_short -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc_short)); $resources -> description_short -> language[0][0] = $n_desc_short; $resources -> description_short -> language[0][0]['id'] = $n_l_id; $resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_title -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; $resources -> meta_title -> language[0][0]['id'] = $n_l_id; $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_description)); $resources -> meta_description -> language[0][0] = $n_meta_description; $resources -> meta_description -> language[0][0]['id'] = $n_l_id; $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> available_now -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_available_now)); $resources -> available_now -> language[0][0] = $n_available_now; $resources -> available_now -> language[0][0]['id'] = $n_l_id; $resources -> available_now -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> available_later -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_available_later)); $resources -> available_later -> language[0][0] = $n_available_later; $resources -> available_later -> language[0][0]['id'] = $n_l_id; $resources -> available_later -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; try { $opt = array('resource' => 'products'); $opt['postXml'] = $xml -> asXML(); $xml = $webService -> add($opt); } catch (PrestaShopWebserviceException $ex) { echo '<b>Error : '.$ex->getMessage().'</b>'; // my log function } } // simple use $n_name = 'New product name3'; $n_desc = 'New product description LONG'; $n_desc_short = 'New product description SHORT'; $n_link_rewrite = 'someone_rewrite'; $n_meta_title = 'meta-title'; $n_meta_description = 'meta desc'; $n_meta_keywords = 'some,one,keywords'; $n_available_now = 'Available for order'; $n_available_later = 'Available from gg/mm/yy'; $n_id_category_default = '9'; $n_id_category = '9'; $n_price = '144.65'; $n_active = '1'; $n_avail4order = '1'; $n_show_price ='1'; $n_l_id = '1'; $n_id_stock_availables = '22'; $n_id_id_product_attribute = '0'; // run PS_new_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later); ?> UPDATE OF PRODUCT <?php define('DEBUG', true); define('PS_SHOP_PATH', 'http://localhost/prestashop1520'); // Root path of your PrestaShop store define('PS_WS_AUTH_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxx'); // Auth key (Get it in your Back Office) require_once ('./PSWebServiceLibrary.php'); $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); function PS_update_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later) { global $webService; try { $opt2 = array('resource' => 'products', 'id'=>'12'); $xml2 = $webService->get($opt2); $resources = $xml2->children()->children(); echo '<br />'; echo 'opt2'.'<br />'; var_dump ($opt2); echo '<br />'; echo '<br />'; echo '<br />'; echo 'xml2'.'<br />'; var_dump ($xml2); echo '<br />'; echo '<br />'; } catch (PrestaShopWebserviceException $e) { // Here we are dealing with errors echo '<b>Error : '.$e->getMessage().'</b>'; } /* unset($resources->position_in_category); unset($resources->manufacturer_name); unset($resources->quantity); //name and link_rewrite must be filled */ unset($resources->position_in_category); unset($resources->manufacturer_name); unset($resources->quantity); //unset($resources -> id); //unset($resources -> position); //unset($resources -> id_shop_default); //unset($resources -> date_add); //unset($resources -> date_upd); //unset($resources->associations->combinations); //unset($resources->associations->product_options_values); //unset($resources->associations->product_features); //unset($resources->associations->stock_availables->stock_available->id_product_attribute); //unset($resources->associations->categories->category->id); //unset($resources-> id_category_default); //$resources->position_in_category = '0'; //unset($resources->position_in_category); //$resources -> position = '0'; //$resources -> id; $resources-> id_manufacturer = '2'; $resources-> id_supplier = '2'; $resources-> id_category_default = $n_id_category_default; $resources-> new = '0'; //condition, new is also a php keyword!! $resources-> cache_default_attribute; $resources-> id_default_image; $resources-> id_default_combination = '0'; $resources-> id_tax_rules_group ='0'; $resources-> id_shop_default; $resources-> reference; $resources-> supplier_reference; $resources-> location; $resources-> width; $resources-> height; $resources-> depth; $resources-> weight; $resources-> quantity_discount; $resources-> ean13; $resources-> upc; $resources-> cache_is_pack; $resources-> cache_has_attachments; $resources-> is_virtual; $resources-> on_sale; $resources-> online_only; $resources-> ecotax; $resources-> minimal_quantity = 1; $resources-> price = $n_price; $resources-> wholesale_price; $resources-> unity; $resources-> unit_price_ratio; $resources-> additional_shipping_cost; $resources-> customizable; $resources-> text_fields; $resources-> uploadable_files; $resources-> active = $n_active; $resources-> available_for_order = $n_avail4order; $resources-> available_date; $resources-> condition; $resources-> show_price = $n_show_price; $resources-> indexed = '1'; $resources-> visibility = 'both'; $resources-> advanced_stock_management='0'; $resources-> date_add; $resources-> date_upd; //$node = dom_import_simplexml($resources -> name -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; //$resources -> name -> language[1][0] = $n_name.'L2'; //$resources -> name -> language[2][0] = $n_name.'L3'; //$resources -> name -> language[0][0]['id'] = $n_l_id; //$resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> description -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; //$resources -> description -> language[0][0]['id'] = $n_l_id; //$resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> description_short -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_desc_short)); $resources -> description_short -> language[0][0] = $n_desc_short; //$resources -> description_short -> language[0][0]['id'] = $n_l_id; //$resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; //$resources -> link_rewrite -> language[1][0] = $n_link_rewrite.'L2'; //$resources -> link_rewrite -> language[2][0] = $n_link_rewrite.'L3'; //$resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; //$resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_title -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; //$resources -> meta_title -> language[0][0]['id'] = $n_l_id; //$resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_description -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_description)); $resources -> meta_description -> language[0][0] = $n_meta_description; //$resources -> meta_description -> language[0][0]['id'] = $n_l_id; //$resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; //$resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; //$resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> available_now -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_available_now)); $resources -> available_now -> language[0][0] = $n_available_now; //$resources -> available_now -> language[0][0]['id'] = $n_l_id; //$resources -> available_now -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> available_later -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_available_later)); $resources -> available_later -> language[0][0] = $n_available_later; //$resources -> available_later -> language[0][0]['id'] = $n_l_id; //$resources -> available_later -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; try { $opt2 = array('resource' => 'products', 'id'=>'12'); echo '<br />'; echo 'opt2A'.'<br />'; var_dump ($opt2); echo '<br />'; echo '<br />'; $opt2['putXml'] = ($xml2->saveXML()); echo '<br />'; echo 'opt2B'.'<br />'; var_dump ($opt2); echo '<br />'; echo 'xml2'.'<br />'; var_dump ($xml2); echo '<br />'; echo '<br />'; echo 'xml2asxml'.'<br />'; var_dump ($xml2->asXML()); echo '<br />'; echo '<br />'; $xml3 = $webService->edit($opt2); //($opt); echo '<br />'; echo '<br />'; echo 'xml4'.'<br />'; var_dump ($xml3); echo '<br />'; echo '<br />'; // if WebService don't throw an exception the action worked well and we don't show the following message echo "Successfully updated."; } catch (PrestaShopWebserviceException $ex) { // Here we are dealing with errors echo '<b>Error : '.$ex->getMessage().'</b>'; } // simple use $n_name = 'New product 5'; $n_desc = 'New product description LONGrewr2'; $n_desc_short = 'New product description SHORTrewr2'; $n_link_rewrite = 'someone_rewriterewr2'; $n_meta_title = 'meta-titlerewr2'; $n_meta_description = 'meta descrewr2'; $n_meta_keywords = 'some,one,keywords,rewr2'; $n_available_now = 'Available for orderrewr2'; $n_available_later = 'Available from gg/mm/yyrewr2'; $n_id_category_default = '9'; $n_id_category = '7'; $n_price = '250.65'; $n_active = '1'; $n_avail4order = '1'; $n_show_price ='1'; $n_l_id = '1'; $n_id_stock_availables = '22'; $n_id_id_product_attribute = '0'; // run PS_update_product($n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later); ?> any ide why i get Error : This call to PrestaShop Web Services failed and returned an HTTP status of 500. That means: Internal Server Error. when i try to use this code? Link to comment Share on other sites More sharing options...
enovo Posted August 8, 2013 Share Posted August 8, 2013 Here an examplo the how to add an image to a product. First, add a product! function PS_new_product($ImageId, $n_id_category_default, $n_id_category, $n_price, $n_active,$n_avail4order,$n_show_price, $n_id_stock_availables ,$n_id_id_product_attribute, $n_l_id, $n_name, $n_desc, $n_desc_short, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords,$n_available_now,$n_available_later, $refernce, $ImagePath) { global $webService; $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/products?schema=blank')); $resources = $xml -> children() -> children(); unset($resources->id); unset($resources->position); unset($resources->id_shop_default); unset($resources->date_add); unset($resources->date_upd); unset($resources->associations->combinations); unset($resources->associations->product_options_values); unset($resources->associations->product_features); unset($resources->associations->stock_availables->stock_available->id_product_attribute); //unset($resources->associations->categories->category->id); //unset($resources-> id_category_default); //$resources->position_in_category = '0'; //unset($resources->position_in_category); //$resources -> position = '0'; //$resources -> id; $resources-> id_manufacturer = '0'; $resources-> id_supplier = '0'; $resources-> id_category_default = $n_id_category_default; $resources-> new = '0'; ; //condition, new is also a php keyword!! $resources-> cache_default_attribute; $resources-> id_default_image; $resources-> id_default_combination = '0'; $resources-> id_tax_rules_group ='0'; //$resources-> id_shop_default; $resources-> reference = $refernce; $resources-> supplier_reference; $resources-> location; $resources-> width ; $resources-> height; $resources-> depth; $resources-> weight; $resources-> quantity_discount; $resources-> ean13; $resources-> upc; $resources-> cache_is_pack; $resources-> cache_has_attachments; $resources-> is_virtual ; $resources-> is_virtual_image = 1 ; $resources-> on_sale; $resources-> online_only; $resources-> ecotax; $resources-> minimal_quantity = 1; $resources-> price = $n_price; $resources-> wholesale_price; $resources-> unity; $resources-> unit_price_ratio; $resources-> additional_shipping_cost; $resources-> customizable; $resources-> text_fields; $resources-> uploadable_files; $resources-> active = $n_active; $resources-> available_for_order = $n_avail4order; $resources-> available_date; $resources-> condition; $resources-> show_price = $n_show_price; $resources-> indexed = '1'; $resources-> visibility = 'both'; $resources-> advanced_stock_management='0'; $resources-> date_add; $resources-> date_upd; $resources->associations->categories->addChild('category')->addChild('id', $n_id_category); //$node = dom_import_simplexml($resources -> name -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; $resources -> name -> language[0][0]['id'] = $n_l_id; $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> description -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = ($n_desc); $resources -> description -> language[0][0]['id'] = $n_l_id; $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> description_short -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_desc_short)); $resources -> description_short -> language[0][0] = ($n_desc_short); $resources -> description_short -> language[0][0]['id'] = $n_l_id; $resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_title -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = ($n_meta_title); $resources -> meta_title -> language[0][0]['id'] = $n_l_id; $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_description -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_description)); $resources -> meta_description -> language[0][0] = substr ( $n_meta_description , 0, 245 )."...";; $resources -> meta_description -> language[0][0]['id'] = $n_l_id; $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> available_now -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_available_now)); $resources -> available_now -> language[0][0] = $n_available_now; $resources -> available_now -> language[0][0]['id'] = $n_l_id; $resources -> available_now -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; //$node = dom_import_simplexml($resources -> available_later -> language[0][0]); //$no = $node -> ownerDocument; //$node -> appendChild($no -> createCDATASection($n_available_later)); $resources -> available_later -> language[0][0] = $n_available_later; $resources -> available_later -> language[0][0]['id'] = $n_l_id; $resources -> available_later -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; try { $opt = array('resource' => 'products'); $opt['postXml'] = $xml -> asXML(); // $opt['postXml'] = str_replace("&", "&", $opt['postXml']); //add the product at the site $xml = $webService -> add($opt); echo "New product inerted number: ". $xml->product->id."<br>"; //Id of the new product in prestashop site //## Add image add_image_to_product($xml->product->id, $refernce, $ImagePath); }catch (PrestaShopWebserviceException $ex) { echo "<b>$refernce ->Error : </b>".$ex->getMessage().'<br>'; //Show errors } } Now how to add image to a product! function add_image_to_product($ProductId, $ImageName, $ImagePath, $Width=640, $Height=640, $ImageQuality=100){ global $ImageTmpPath; //Make url $url = "http://mysite/api/images/products/$ProductId"; $resizeObj = new resize($ImagePath); //Resize iamge $resizeObj -> resizeImage($Width, $Height); //Save resized temp image $resizeObj -> saveImage("$ImageTmpPath/$ImageName", $ImageQuality); //Uplodad image to a product $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); //curl_setopt($ch, CURLOPT_PUT, true); // Un-commet to edit an image curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':'); curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'."$ImageTmpPath/$ImageName")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); //Delete temp image unlink("$ImageTmpPath/$ImageName"); } Link to comment Share on other sites More sharing options...
enovo Posted August 9, 2013 Share Posted August 9, 2013 How to add a product attribute to a product? any idea? Link to comment Share on other sites More sharing options...
enovo Posted August 9, 2013 Share Posted August 9, 2013 Solotion to the first question! If you use only one category use the next declaration: $resources->associations->categories->category->id = $n_id_category; Use the next syntax to asociate more cateogries $resources->associations->categories->addChild('category')->addChild('id', 2); $resources->associations->categories->addChild('category')->addChild('id', 3); $resources->associations->categories->addChild('category')->addChild('id', N); 1 Link to comment Share on other sites More sharing options...
mayek Posted March 20, 2014 Share Posted March 20, 2014 My solution is: $resources -> link_rewrite= ' ' ; // Clean this object $resources -> link_rewrite -> addChild('language' , 'THERE WAS NAME OF PRODUCT/CATEGORY'); $resources -> link_rewrite -> language->AddAttribute('id', '1'); Link to comment Share on other sites More sharing options...
gamdev Posted July 9, 2015 Share Posted July 9, 2015 (edited) Solotion to the first question! If you use only one category use the next declaration: $resources->associations->categories->category->id = $n_id_category; Use the next syntax to asociate more cateogries $resources->associations->categories->addChild('category')->addChild('id', 2); $resources->associations->categories->addChild('category')->addChild('id', 3); $resources->associations->categories->addChild('category')->addChild('id', N); I need to add categories but I need to delete the old categories assigned to that product. how I can do ? Edited July 9, 2015 by gamdev (see edit history) Link to comment Share on other sites More sharing options...
gamdev Posted September 29, 2015 Share Posted September 29, 2015 any ide why i get Error : This call to PrestaShop Web Services failed and returned an HTTP status of 500. That means: Internal Server Error. when i try to use this code? somehow solved the error 500? Link to comment Share on other sites More sharing options...
Recommended Posts