me.yasin Posted May 18, 2013 Share Posted May 18, 2013 I am using this sample code to add product in prestashop but I don't know how to add product attributes like quantity, color, size with this product. I have array of color and size that I want to add with this product if that attribute is not added first add these attributes then link with this product. <?php define('DEBUG', false); define('_PS_DEBUG_SQL_', false); define('PS_SHOP_PATH', 'http://localhost/prestashop_demo'); define('PS_WS_AUTH_KEY', '2LM2T4H56352K1L4FZ40V9H6UPPQD4WE'); require_once ('PSWebServiceLibrary.php'); //include_once ('core_config.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-> quantity = '50'; $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 = 10; $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>'; $trace = $ex->getTrace(); print_r($trace); // my log function } } // simple use $n_name = 'New product name 5'; $n_desc = 'New product description LONG'; $n_desc_short = 'New product description SHORT'; $n_link_rewrite = 'someone_rewrite_5'; $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 = '2'; $n_id_category = '2'; $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); Link to comment Share on other sites More sharing options...
xabikip Posted February 5, 2015 Share Posted February 5, 2015 you got what you wanted? I need some similar. Link to comment Share on other sites More sharing options...
xabikip Posted February 10, 2015 Share Posted February 10, 2015 I have managed to add the quantity. Link to comment Share on other sites More sharing options...
Totti Posted May 4, 2015 Share Posted May 4, 2015 News ? Link to comment Share on other sites More sharing options...
Radhanatha Posted July 12, 2016 Share Posted July 12, 2016 //add quantity and product image// public function addProduct(){ // simple use $n_name = 'demo tshirt'; $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); $products = array(); $opt['resource'] = 'products'; $opt['display'] = 'full'; $opt['filter']['name'] = '['.$n_name.']%'; $xml = $webService->get($opt); $resources = $xml->products->children(); $array = json_decode(json_encode((array)$resources), TRUE); if(empty($array)){ $n_desc = 'New product description LONG'; $n_desc_short = 'New product description SHORT'; $n_link_rewrite = 'someone_rewrite_5'; $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 = '2'; $n_id_category = '2'; $n_price = '144.65'; $n_active = '1'; $n_avail4order = '1'; $n_show_price ='1'; $n_l_id = Context::getContext()->language->id; //$n_l_id = '1'; $n_id_stock_availables = '22'; $n_id_id_product_attribute = '0'; $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); $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-> quantity = '50'; $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 = 10; $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; $shop = (int)Context::getContext()->shop->id; try { $opt = array('resource' => 'products'); $opt['postXml'] = $xml -> asXML(); $xml = $webService -> add($opt); $resources = $xml->product->children(); $array = json_decode(json_encode((array)$resources), TRUE); $msg = $this->addImageAndQunatinty($array); echo $msg; } catch (PrestaShopWebserviceException $ex) { echo '<b>Error : '.$ex->getMessage().'</b>'; $trace = $ex->getTrace(); print_r($trace); // my log function } }else{ echo $msg='Name already exit'; } } public function addImageAndQunatinty($array){ $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); $id_product = $array['id']; //product image add $url ="http://localhost:8080/api/new/images/slide/product-slide-img3.jpg"; $image = new Image(); $image->id_product = $id_product; $image->position = Image::getHighestPosition($id_product) + 1; $image->cover = true; // or false; if (($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add()) { $image->associateTo($shop); if (!self::copyImg($id_product, $image->id, $url, 'products', false)) { $image->delete(); } } //end //start quantity add $stockId =$array['associations']['stock_availables']['stock_available']['id']; $AttributeId =$array['associations']['stock_availables']['stock_available']['id_product_attribute']; $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/stock_availables?schema=blank')); $resources = $xml -> children() -> children(); $resources->id = $stockId; $resources->id_product = $id_product; $resources->quantity = 10000000; $resources->id_shop = 1; $resources->out_of_stock=1; $resources->depends_on_stock = 0; $resources->id_product_attribute=$AttributeId; try { $opt = array('resource' => 'stock_availables'); $opt['putXml'] = $xml->asXML(); $opt['id'] = $stockId; $xml = $webService->edit($opt); return $msg='Product added successfully'; }catch (PrestaShopWebserviceException $ex) { echo "<b>Error al setear la cantidad ->Error : </b>".$ex->getMessage().'<br>'; } } public function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true){ $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import'); $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES')); switch ($entity) { default: case 'products': $image_obj = new Image($id_image); $path = $image_obj->getPathForCreation(); break; case 'categories': $path = _PS_CAT_IMG_DIR_.(int)$id_entity; break; case 'manufacturers': $path = _PS_MANU_IMG_DIR_.(int)$id_entity; break; case 'suppliers': $path = _PS_SUPP_IMG_DIR_.(int)$id_entity; break; } $url = urldecode(trim($url)); $parced_url = parse_url($url); if (isset($parced_url['path'])) { $uri = ltrim($parced_url['path'], '/'); $parts = explode('/', $uri); foreach ($parts as &$part) { $part = rawurlencode($part); } unset($part); $parced_url['path'] = '/'.implode('/', $parts); } if (isset($parced_url['query'])) { $query_parts = array(); parse_str($parced_url['query'], $query_parts); $parced_url['query'] = http_build_query($query_parts); } if (!function_exists('http_build_url')) { require_once(_PS_TOOL_DIR_.'http_build_url/http_build_url.php'); } $url = http_build_url('', $parced_url); $orig_tmpfile = $tmpfile; if (Tools::copy($url, $tmpfile)) { // Evaluate the memory required to resize the image: if it's too much, you can't resize it. if (!ImageManager::checkImageMemoryLimit($tmpfile)) { @unlink($tmpfile); return false; } $tgt_width = $tgt_height = 0; $src_width = $src_height = 0; $error = 0; ImageManager::resize($tmpfile, $path.'.jpg', null, null, 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height); $images_types = ImageType::getImagesTypes($entity, true); if ($regenerate) { $previous_path = null; $path_infos = array(); $path_infos[] = array($tgt_width, $tgt_height, $path.'.jpg'); foreach ($images_types as $image_type) { $tmpfile = self::get_best_path($image_type['width'], $image_type['height'], $path_infos); if (ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height'], 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height)) { // the last image should not be added in the candidate list if it's bigger than the original image if ($tgt_width <= $src_width && $tgt_height <= $src_height) { $path_infos[] = array($tgt_width, $tgt_height, $path.'-'.stripslashes($image_type['name']).'.jpg'); } if ($entity == 'products') { if (is_file(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$id_entity.'.jpg')) { unlink(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$id_entity.'.jpg'); } if (is_file(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$id_entity.'_'.(int)Context::getContext()->shop->id.'.jpg')) { unlink(_PS_TMP_IMG_DIR_.'product_mini_'.(int)$id_entity.'_'.(int)Context::getContext()->shop->id.'.jpg'); } } } if (in_array($image_type['id_image_type'], $watermark_types)) { Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity)); } } } } else { @unlink($orig_tmpfile); return false; } unlink($orig_tmpfile); return true; } Link to comment Share on other sites More sharing options...
Recommended Posts