Jump to content

PrestaShop WebService - Price Update for various products


Recommended Posts

 

I have this to update the PrestaShop price for one product with the WebService.

    <html><head><title>CRUD Data Transfer - Update example</title></head><body>
<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true);
define('PS_SHOP_PATH', 'https://my.domain.com');
define('ID_PRODUCT', 1);
define('PS_WS_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX');
require_once('./PSWebServiceLibrary.php');
@ini_set('display_errors', 'on');
try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array('resource' => 'products');
$opt['id']=ID_PRODUCT;
$xml = $webService->get($opt);
echo "Successfully recived data.";
     /* List of nodes that can't modify
     *
     *  - "manufacturer_name"
     *  - "position_in_category"
     *  - "quantity"
     *  - "type"
     */
    unset($xml->children()->children()->manufacturer_name);
    unset($xml->children()->children()->position_in_category);
    unset($xml->children()->children()->quantity);
    unset($xml->children()->children()->type);
   $xml->children()->children()->price = 111.0; // <-- new price!
//Load new data to query generator
$opt['putXml']=$xml->asXML();
$xml = $webService->edit($opt);
// 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
$trace = $ex->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error<br />'.$ex->getMessage();
}

?>
</body></html>

How can i do the same, but for all the products in a MySQL table?

I have all the products in a MySQL table with the ID Key as Source_ID.

Appreciate any help!

Regards

Link to comment
Share on other sites

  • 4 weeks later...
19 hours ago, a17000 said:

Hi,

If you have mysql access,

make a php list all product from table mysql <your_bd>.ps_product 

and make a loop in php for each product wwith id_product  

I don't understand the problem??

 

Hi, maybe for you it's most easy. haha

 

For me it's a little more difficult, i am a begginer in PHP, so...

 

Can i see an example of what you mean?

 

Let me know and thank you

Link to comment
Share on other sites

ok, another way it's to make a loop via api 

$webService->get(array('url' => PS_SHOP_PATH . '/api/products?limit=10'));
$resources = $xml -> children();
if(!empty($resources)){
	foreach ($resources as $product_obj) {
		echo $product_obj->id.'<br>';
	}
}

more cleanB) think to delete the limit

Link to comment
Share on other sites

16 hours ago, a17000 said:

So, that ok now?

 

 

Hello,

 

I'm trying it, but for the moment i only have the get for the products with their id i think..

 

This is the code :

 

<html><head><title>CRUD Data Transfer - Update example</title></head><body>
<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true);
define('PS_SHOP_PATH', 'https://domain.com');
//define('ID_PRODUCT', 1);
define('PS_WS_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
require_once('./PSWebServiceLibrary.php');
@ini_set('display_errors', 'on');
try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array('resource' => 'products');
//$opt['id']=ID_PRODUCT;
$xml = $webService->get($opt);
$webService->get(array('url' => PS_SHOP_PATH . '/api/products?limit=10'));
$resources = $xml -> children();
if(!empty($resources)){
	foreach ($resources as $product_obj) {
		echo $product_obj->id.'<br>';
	}
}
echo "Successfully recived data.";
	 /* List of nodes that can't modify
	 *
	 *  - "manufacturer_name"
	 *  - "position_in_category"
	 *  - "quantity"
	 *  - "type"
	 */
	unset($xml->children()->children()->manufacturer_name);
	unset($xml->children()->children()->position_in_category);
	unset($xml->children()->children()->quantity);
	unset($xml->children()->children()->type);
   $xml->children()->children()->price = 211.0; // <-- new price!
//Load new data to query generator
$opt['putXml']=$xml->asXML();
$xml = $webService->edit($opt);
// 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
$trace = $ex->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error<br />'.$ex->getMessage();
}

?>
</body></html>

 

But the output is only that:

 

HTTP REQUEST HEADER
GET /api/products HTTP/1.1
Host: domain.com
Authorization: Basic VVgxWE5GRU1GMVE3M1E3MkdDTUQ3UFZNWEFGR0ZTVFo6
Accept: */*

HTTP RESPONSE HEADER
HTTP/1.1 200 OK
Date: Fri, 19 Jan 2018 07:27:33 GMT
Server: Apache
Access-Time: 1516346854
X-Powered-By: PrestaShop Webservice
PSWS-Version: 1.6.1.17
Execution-Time: 0.007
Content-Sha1: 5cd2f412e9f89dffbfe6d4e5ef5037d3efeda467
Set-Cookie: PrestaShop-d0e2f1387143146e89d1940bce2103aa=0OkB9ZIt2WtPQ3tBFukD%2BOiAj4C47IuWek%2FKXnL%2B11Niq3zwUzPs2ylBRS25Ui6apbr5T01K6WkTgk5shuDT5JKAptOYvN3IwNUXAIzlF88%3D000079; expires=Thu, 08-Feb-2018 07:27:34 GMT; Max-Age=1728000; path=/; domain=domain.com; HttpOnly
Vary: Accept-Encoding,User-Agent
Connection: close
Transfer-Encoding: chunked
Content-Type: text/xml;charset=utf-8
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product id="2" xlink:href="http://domain.com/api/products/2"/>
<product id="1" xlink:href="http://domain.com/api/products/1"/>
<product id="3" xlink:href="http://domain.com/api/products/3"/>
</products>
</prestashop>
HTTP REQUEST HEADER
GET /api/products?limit=10 HTTP/1.1
Host: domain.com
Authorization: Basic VVgxWE5GRU1GMVE3M1E3MkdDTUQ3UFZNWEFGR0ZTVFo6
Accept: */*

HTTP RESPONSE HEADER
HTTP/1.1 200 OK
Date: Fri, 19 Jan 2018 07:27:34 GMT
Server: Apache
Access-Time: 1516346854
X-Powered-By: PrestaShop Webservice
PSWS-Version: 1.6.1.17
Execution-Time: 0.038
Content-Sha1: 5cd2f412e9f89dffbfe6d4e5ef5037d3efeda467
Set-Cookie: PrestaShop-d0e2f1387143146e89d1940bce2103aa=0OkB9ZIt2WtPQ3tBFukD%2BOiAj4C47IuWek%2FKXnL%2B11Niq3zwUzPs2ylBRS25Ui6apbr5T01K6WkTgk5shuDT5JKAptOYvN3IwNUXAIzlF88%3D000079; expires=Thu, 08-Feb-2018 07:27:34 GMT; Max-Age=1728000; path=/; domain=domain.com; HttpOnly
Vary: Accept-Encoding,User-Agent
Connection: close
Transfer-Encoding: chunked
Content-Type: text/xml;charset=utf-8
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product id="2" xlink:href="http://domain.com/api/products/2"/>
<product id="1" xlink:href="http://domain.com/api/products/1"/>
<product id="3" xlink:href="http://domain.com/api/products/3"/>
</products>
</prestashop>

Successfully recived data.Other error
Bad parameters given

 

How can i do for the webservice, now is only give me the products id.

 

I need he update all the prices for the id (1,2,3 in the example, but if i add a product with id 4 i wan't also to be updated.) 

(logically, i need to have also the products with id 1,2,3 in my mySQL table (my ID key is source_ID) and i have succesfully the id's 1,2 and 3.

 

How can i do that? 

 

As i can see for the moment the only thing i have is the id's in the PrestaShop.

 

Now i need the script updates all theese PrestaShop id's with the prices in my mySQL table with the ID Key as source_ID.

 

Let me know please,


And really appreciate your help.

 

Regards

Edited by Alex Sanchez (see edit history)
Link to comment
Share on other sites

Why not :

$webService->get(array('url' => PS_SHOP_PATH . '/api/products?limit=10'));
$resources = $xml -> children();
if(!empty($resources)){
	foreach ($resources as $product_obj) {
		//echo $product_obj->id.'<br>';
		$product->price = 211.0
        if (($product->validateFields(false, true)) === true && ($product->validateFieldsLang(false, true)) === true && $product->save()) {
                    echo $product_obj->id.' price udated!<br>';
                }
	}
}

 

Link to comment
Share on other sites

  • 1 month later...

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
×
×
  • Create New...