Jump to content

Error al setear la cantidad ->Error : This call to PrestaShop Web Services failed and returned an HT


Recommended Posts

Hi

i wanna add product to prestashop  1.6.0.6 via webservice My code is :

define('PS_SHOP_PATH', 'www.-----.com');
define('PS_WS_AUTH_KEY', 'xxxxxxxxxxx');
define('DEBUG', false);
require_once('ios/PSWebServiceLibrary.php');
function set_product_quantity($ProductId, $StokId, $AttributeId,$quantity){
  $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/stock_availables?schema=blank'));
$resources = $xml -> children() -> children();
$resources->id = $StokId;
$resources->id_product  = $ProductId;
$resources->quantity =$quantity;
$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'] = $StokId ;
    $xml = $webService->edit($opt);
}
  catch (PrestaShopWebserviceException $ex) {
    echo "<b>Error al setear la cantidad  ->Error : </b>".$ex->getMessage().'<br>';
  }
}
 
if($values['pubblica']==1){
 
  global $host;
  global $sys_dbname;
  global $user;
  global $pwd;
  
  $mysqli = new mysqli($host,$user,$pwd,$sys_dbname);
  
  $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
/*  
  try{
  $xml = $webService -> get(array('url' => PS_SHOP_PATH . "/api/products?filter[name]=".urlencode($values["prod"]))); 
    foreach($xml->xpath("//products") as $node){
      $pr=$node->attributes();
      $webService -> delete(array("resource"=>"products","id"=>(int)$pr["id"]));
    }
  catch(Exception $e){echo "NON PRESENTE";}
 
 
$xml = $webService -> get(array('url' => PS_SHOP_PATH . "api/products"));
foreach($xml->xpath("//product") as $node){
echo (int)$imm["id"]."-";
$imm=$node->attributes();
$webService -> delete(array("resource"=>"products","id"=>(int)$imm["id"]));
}
  } 
  catch(Exception $e){echo "ERRORE";}
*/
    $n_id=$values["id_prodotto_prestashop"];
    $n_name = $values['prod'];
$n_desc = $values['Note'];
$n_desc_short = $values['descrizione'];
$n_link_rewrite =str_replace(" ","_",$values['prod']);
$n_meta_title =  $values['prod'];
$n_meta_description =  $values['prod'];
$n_meta_keywords =  $values['prod'];
$n_available_now = 1;
$n_available_later = 1;
$n_id_category = $values['categoria_ecommerce'];
$n_price = str_replace(",",".",$values['Prezzo']);
$n_active =1 ;
$n_avail4order =1 ;
$n_show_price =1;
$n_l_id = '';
$n_id_stock_availables ='' ;
$n_id_id_product_attribute ='' ;
 
  $img="";
if($values['immagine']!=""){
  $img_arr=json_decode($values['immagine']);
$img=$img_arr[0]->name;
  } else $img="files/logo_alberino.jpg";
    $quantity=$values['quantita'];
    $discount=(float)str_replace(",",".",$values['Sconto'])*100;
 
$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);
$resources-> id_manufacturer = '1';
$resources-> id_supplier = '1';
$resources-> id_category_default = $n_id_category;
$resources-> cache_default_attribute;
$resources-> id_default_image;
$resources-> id_default_combination = '0';
$resources-> quantity_discount;
  $resources-> reference = 'IOS'; 
  $resources-> minimal_quantity = 1;
  $resources-> price = $n_price;
  $resources-> active = $n_active;
  $resources-> available_for_order = $n_avail4order;
  $resources-> show_price = $n_show_price;
  $resources-> indexed = '1';
  $resources-> visibility = 'both';
  $resources-> advanced_stock_management='0';
  $resources-> associations->categories->addChild('category')->addChild('id', $n_id_category);
  
  if($n_link_rewrite!=""){
  $node = dom_import_simplexml($resources -> link_rewrite -> language);
  $no = $node -> ownerDocument;
  $node -> appendChild($no -> createCDATASection(str_replace(array("/"," "),array("_","_"),urlencode($n_link_rewrite))));
  }
  if($n_name!=""){
  $node = dom_import_simplexml($resources -> name-> language);
  $no = $node -> ownerDocument;
  $node -> appendChild($no -> createCDATASection(urlencode($n_name)));
  }
  
 
  $node = dom_import_simplexml($resources -> description -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection(urlencode($n_desc)));
$resources -> description -> language[0][0]['id'] = 1;
$resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . 1;
 
$node = dom_import_simplexml($resources -> description_short -> language[0][0]);
$no = $node -> ownerDocument;
$node -> appendChild($no -> createCDATASection(urlencode($n_desc_short)));
$resources -> description_short -> language[0][0]['id'] = 1;
$resources -> description_short -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . 1;
  try {
    // inserimento prodotto
    $opt = array('resource' => 'products');
 
    $opt['postXml'] = $xml -> asXML();
 
    $xml = $webService -> add($opt);
    $id_prodotto=$xml->children()->children()->id;
    //quantita
 
    foreach ($xml->product->associations->stock_availables->stock_available as $item) {
 
 set_product_quantity($id_prodotto, $item->id, $item->id_product_attribute,$quantity);
  }
 
    //inserimento id prestashop nel db
    //db_exec("update prodotti set id_prodotto_prestashop='".$id_prodotto."' where prodotto='".$n_name."'",$conn);
$rs_prodotti1=$mysqli->query("update prodotti set id_prodotto_prestashop='".$id_prodotto."' where prodotto='".$mysqli->escape_string($n_name)."'");
    //inserimneto immagine 
$id_immagine="";
    $data_img= array('image'=>"@".$img.";type=image/jpg");
    $link_prodotto='/api/images/products/'.$id_prodotto;
    $risorsa_prodotto="images/products/".$id_prodotto;
 /*try{
$xml = $webService -> get(array('url' => PS_SHOP_PATH . $link_prodotto));
foreach($xml->xpath("//declination") as $node){
$imm=$node->attributes();
$webService -> delete(array("resource"=>$risorsa_prodotto,"id"=>(int)$imm["id"]));
}
  } 
  catch(Exception $e){echo "ERRORE";}
  */
  //$data_img= array('image'=>"@".$img.";type=image/jpeg");
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'www.-------.com'.$link_prodotto);
  curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_img);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  if(curl_exec($ch) === false)
  {echo 'Errore : '.curl_error($ch).'<br>';}
  else
  { 
    /*$xml = $webService -> get(array('url' => PS_SHOP_PATH . $link_prodotto)); 
    foreach($xml->xpath("//declination") as $node){
      $imm=$node->attributes();
      $id_immagine= (int)$imm["id"];
    }*/
  }
 
  curl_close($ch);
//edito prodotto per inserimento id immagine
/*$xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/products/'.$id_prodotto.'?schema=blank'));
$resources = $xml -> children() -> children();
$resources ->id_default_image=$id_immagine;
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->position_in_category);
unset($resources->manufacturer_name);
unset($resources->quantity);
$webService->edit(array('resource' => 'products', 'id' =>$id_prodotto,'putXml'=>$xml -> asXML()));*/
if($discount!=0) {
            $webService->get(array('resource' => 'specific_prices'));
            $opt['resource'] = 'specific_prices?schema=blank';
$xml=$webService->get($opt);
$resources=$xml->children()->children();
unset($resources->id);
$resources->id_shop=0;
$resources->id_cart=0;
$resources->id_currency=0;
$resources->id_country=0;
$resources->id_group=0;
$resources->id_customer=0;
$resources->price=-1;
$resources->from_quantity=1;
$resources->from="0000-00-00 00:00:00";
$resources->to="0000-00-00 00:00:00";
$resources->id_product=$id_prodotto;
$resources->reduction=$discount/100;
$resources->reduction_type="percentage";
$opt2 = array('resource' => 'specific_prices');
$opt2['postXml'] = $xml -> asXML();
$xmladd=$webService->add($opt2);
                $id_sconto=$xmladd->children()->children()->id;
//db_exec(" update prodotti set id_sconto ='".$id_sconto."' where id_prodotto_prestashop = '".$id_prodotto."'",$conn);
$mysqli->query(" update prodotti set id_sconto ='".$id_sconto."' where id_prodotto_prestashop = '".$id_prodotto."'");
       }
      } 
      catch (PrestaShopWebserviceException $ex) 
 {
echo '<b>Error : '.$ex->getMessage().'</b>';
$trace = $ex->getTrace();
print_r($trace);
}
 
}
Edited by ElMarley (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...