Affichage des articles dont le libellé est quantity. Afficher tous les articles
Affichage des articles dont le libellé est quantity. Afficher tous les articles

samedi 6 juin 2015

Prestashop webservice: update product and combination quantity


In prestashop, you can have products with combinations, in this case, the product quantity will get the combinations quantities sum. This is a function that uses the prestashop webservice to update the product/combination quantity, the difference here between product and combination, with product quantity, you have the product_attribute_id equal to 0 and for combination it's the id_product_attribute (the combination id)

function set_product_quantity($ProductId, $StokId, $AttributeId,$quantity){
 global $webService;
 $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);
  $monfichier = fopen('log_stock.txt', 'a');
  fputs($monfichier, 'Date: '.date('d m y: hh:mm').PHP_EOL);
  fputs($monfichier, 'Exécution du script de mise à jour de stock'.PHP_EOL);
  fputs($monfichier, 'Quantity updated for product '.$ProductId.' product_attribute_id '. $AttributeId.PHP_EOL);
  fclose($monfichier);
  echo 'Quantity updated for product '.$ProductId.' product_attribute_id '. $AttributeId.' id shop: '.$resources->id_shop.'
';
 }catch (PrestaShopWebserviceException $ex) {
  echo "Error con la cantidad  ->Error : ".$ex->getMessage().'
';
 }
}
You should declare this includes,
require_once('./PSWebServiceLibrary.php');
require('../config/config.inc.php');
You can find the file PSWebServiceLibrary.php in github And defines this constants
define('DEBUG', true);
define('PS_SHOP_PATH', 'yourShopPath');
define('PS_WS_AUTH_KEY', 'Your_Auth_Key');
You should generate your key in prestashop webservice back office tab To call this function
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
set_product_quantity($ProductId, $StokId, $AttributeId,$quantity);
$AttributeId should be 0 to change product quantity (parent) and combination id to change combination quantity

  Finally this is a function to get combination by product id and ean
function getIdProductAttribute($id_product, $ean){
 
$sql = 'SELECT `id_product_attribute` FROM `'._DB_PREFIX_.'product_attribute` pa

WHERE ean13 like \''.$ean.'\'  ';

$id_product_attribute = Db::getInstance()->getValue($sql);

$sql_stock = 'SELECT `id_stock_available` FROM `'._DB_PREFIX_.'stock_available` WHERE `id_product`='.$id_product.' and `id_product_attribute`='.$id_product_attribute;
$id_stock = Db::getInstance()->getValue($sql_stock);

return array('id_product_attribute'=>$id_product_attribute,'id_stock'=>$id_stock);
}
 
Hope this helps! Any suggestions or questions are welcome :)

jeudi 4 juin 2015

Update product quantity Ajax prestashop

If you search for the js code to update quantity in prestashop 1.6, you can find it under prestashop/js/admin-products.js line 1451

This code is responsible for product quantity update, in quantities tab under product admin page.
$('.available_quantity').find('input').change(function(e, init_val){
self.ajaxCall({actionQty: 'set_qty', 
id_product_attribute: $(this).parent().attr('id').split('_')[1], value: $(this).val()});
});
 
 
You can search for ajaxCall in the same file,
 
 
product_tabs['Quantities'] = new function(){
 var self = this;
 this.ajaxCall = function(data){
  data.ajaxProductQuantity = 1;
  data.id_product = id_product;
  data.token = token;
  data.ajax = 1;
  data.controller = "AdminProducts";
  data.action = "productQuantity";

  $.ajax({
   type: "POST",
   url: "ajax-tab.php",
   data: data,
   dataType: 'json',
   async : true,
   beforeSend: function(xhr, settings)
   {
    $('.product_quantities_button').attr('disabled', 'disabled');
   },
   complete: function(xhr, status)
   {
    $('.product_quantities_button').removeAttr('disabled');
   },
   success: function(msg)
   {
    if (msg.error)
    {
     showErrorMessage(msg.error);
     return;
    }
    showSuccessMessage(quantities_ajax_success);
   },
   error: function(jqXHR, textStatus, errorThrown)
   {
    if (textStatus != 'error' || errorThrown != '')
     showErrorMessage(textStatus + ': ' + errorThrown);    
   }
  });
 };
 
Hope this helps!