Jump to content

Updating Product Positions In Categories Using Webservice API (1.6.0.14)


hygap

Recommended Posts

HI All

I wonder if anyone could shed a little light on the trouble I am having.

I am trying to update a products position in it's category using Prestashop's webservice. From reading around I found this was a problem in the past and a subsequent bug was filed ( http://forge.prestashop.com/browse/PSCSX-1563 ) and fixed.

From this I understand the only way to update the position would be to set the id_category_default to the category you wish to affect and position_in_category on a product within that category? I have included a striped down code snippet below to show how I am trying to achieve this.

$url = 'myUrl';
$key = 'myKey';
$productID = 73;

$webService = new PrestaShopWebservice($url, $key, true);
try {
    // Get product
    $optIn = array('resource' => 'products');
    $optIn['id'] = $productID;
    $xmlIn = $webService->get($optIn);
    $prestashopProduct = $xmlIn->product;

    // Set new position
    $prestashopProduct->position_in_category = 4;

    // Remove values that cannot be updated.
    unset($prestashopProduct->manufacturer_name);
    unset($prestashopProduct->quantity);

    // Setup update
    $optOut = array('resource' => 'products');
    $optOut['putXml'] = '<?xml version="1.0" encoding="UTF-8"?><prestashop xmlns:xlink="http://www.w3.org/1999/xlink">' . $prestashopProduct->asXML() . '</prestashop>';
    $optOut['id'] = $prestashopProduct->id;
    $webService->edit($optOut);

} catch (PrestaShopWebserviceException $e) {
    echo 'Other error: ' . $e->getMessage() . '<br />';
}

When I run this code from the API debug output I can see that I am receiving and updating the product then sending it back to the server without any error codes returning.

However the return body from the update PUT always shows the position_in_category updated to 0 despite the position I send. I have included snippets to show this below.

Please Note: The API is returning no warnings and that the category has over 4 items in it so updating to this position should be fine. I have tried surrounding the number in <![CDATA[]]> tags and also removing the notFilterable="true" attribute, all to no avail.

HTTP REQUEST HEADER

GET /prestashop/api/products/73 HTTP/1.1
Authorization: Basic **REMOVEDFORFORUMPOST**
Host: 192.168.0.2:8080
Accept: */*

HTTP RESPONSE HEADER

HTTP/1.1 200 OK
Date: Fri, 10 Jul 2015 08:45:29 GMT
Server: Apache/2.2.22 (Win32) mod_ssl/2.2.22 OpenSSL/0.9.8t PHP/5.4.5
X-Powered-By: PrestaShop Webservice
Access-Time: 1436517929
PSWS-Version: 1.6.0.14
Execution-Time: 0.041
Content-Sha1:**REMOVEDFORFORUMPOST**
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">
<product>
	<id><![CDATA[73]]></id>
	<id_manufacturer><![CDATA[0]]></id_manufacturer>
	<id_supplier><![CDATA[0]]></id_supplier>
	<id_category_default xlink:href="https://192.168.0.2:8080/prestashop/api/categories/41"><![CDATA[41]]></id_category_default>
	<new></new>
	<cache_default_attribute><![CDATA[344]]></cache_default_attribute>
	<id_default_image xlink:href="https://192.168.0.2:8080/prestashop/api/images/products/73/643" notFilterable="true"><![CDATA[643]]></id_default_image>
	<id_default_combination xlink:href="https://192.168.0.2:8080/prestashop/api/combinations/344" notFilterable="true"><![CDATA[344]]></id_default_combination>
	<id_tax_rules_group><![CDATA[0]]></id_tax_rules_group>
	<position_in_category notFilterable="true"><![CDATA[2]]></position_in_category>
	<manufacturer_name notFilterable="true"></manufacturer_name>
	<quantity notFilterable="true"><![CDATA[0]]></quantity>
    ... rest of responce removed as not needed


HTTP REQUEST HEADER

PUT /prestashop/api/products/73 HTTP/1.1
Authorization: Basic **REMOVEDFORFORUMPOST**
Host: 192.168.0.2:8080
Accept: */*
Content-Length: 10192
Content-Type: application/x-www-form-urlencoded

HTTP RESPONSE HEADER

HTTP/1.1 200 OK
Date: Fri, 10 Jul 2015 08:45:29 GMT
Server: Apache/2.2.22 (Win32) mod_ssl/2.2.22 OpenSSL/0.9.8t PHP/5.4.5
X-Powered-By: PrestaShop Webservice
Access-Time: 1436517930
PSWS-Version: 1.6.0.14
Execution-Time: 0.329
Content-Sha1: **REMOVEDFORFORUMPOST**
Transfer-Encoding: chunked
Content-Type: text/xml;charset=utf-8

XML SENT

<?xml version="1.0" encoding="UTF-8"?><prestashop xmlns:xlink="http://www.w3.org/1999/xlink"><product>
	<id>73</id>
	<id_manufacturer>0</id_manufacturer>
	<id_supplier>0</id_supplier>
	<id_category_default xlink:href="https://192.168.0.2:8080/prestashop/api/categories/41">41</id_category_default>
	<new/>
	<cache_default_attribute>344</cache_default_attribute>
	<id_default_image xlink:href="https://192.168.0.2:8080/prestashop/api/images/products/73/643" notFilterable="true">643</id_default_image>
	<id_default_combination xlink:href="https://192.168.0.2:8080/prestashop/api/combinations/344" notFilterable="true">344</id_default_combination>
	<id_tax_rules_group>0</id_tax_rules_group>
	<position_in_category notFilterable="true">4</position_in_category>
	
	
	<type notFilterable="true">simple</type>
	<id_shop_default>1</id_shop_default>
	... rest of responce removed as not needed
	
	
RETURN HTTP BODY

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<product>
	<id><![CDATA[73]]></id>
	<id_manufacturer><![CDATA[0]]></id_manufacturer>
	<id_supplier><![CDATA[0]]></id_supplier>
	<id_category_default xlink:href="https://192.168.0.2:8080/prestashop/api/categories/41"><![CDATA[41]]></id_category_default>
	<new></new>
	<cache_default_attribute><![CDATA[344]]></cache_default_attribute>
	<id_default_image xlink:href="https://192.168.0.2:8080/prestashop/api/images/products/73/643" notFilterable="true"><![CDATA[643]]></id_default_image>
	<id_default_combination xlink:href="https://192.168.0.2:8080/prestashop/api/combinations/344" notFilterable="true"><![CDATA[344]]></id_default_combination>
	<id_tax_rules_group><![CDATA[0]]></id_tax_rules_group>
	<position_in_category notFilterable="true"><![CDATA[0]]></position_in_category>
	<manufacturer_name notFilterable="true"></manufacturer_name>
	<quantity notFilterable="true"></quantity>
	... rest of responce removed as not needed

So my question is why is this happening and how can I:

A: Update the position of a product in a specific category?
    * Is there a better method to do this using another part of the API?

B: Update a product without setting its category to 0?
    * On this point I have tried un-setting/removing the position_in_category variable from the PUT. The response is still 0.

To give wider context as to why I am looking for a solution here; I am trying to loop all the products in my site and update the price, descriptions e.t.c from a separate main stock system using the webservice. Every time I run the loop it destroys any custom ordering of stock within each category. I would like my positions to stay intact during the update and or the ability to write a routine to order them using parameters from my other system in the future.

Version details:
PSWS-Version: 1.6.0.14

Thanks for any help in advance - Rich

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

HI All

After digging though the Prestashop source I think I have discovered the issue.

It seems that if you pass the object back just as it comes out (with just the position_in_category changed) Prestashop does indeed update the category to the correct position. This is achieved in the function setWsPositionInCategory in the main Product class.

However after calling this function the webservice gets on to process the contents of <associations><categories> ... </categories></associations> which I am also passing back. This causes function setWsCategories to be called in the main Product class. This lovely method deletes all your category associations for that product and then recreates them again just whacking the position to the end of the category it is added to.

So the solution is to unset this in the update which can be achieved by adding the following before passing the product back to be updated.

unset($prestashopProduct->associations->categories);

This stops the webservice from updating all the cats. Just runing setWsPositionInCategory which updates everything fine.

 

 

But just when I thought this was a total waste of my time I have at least spyed a small error in the Prestashop source code. Which due to some lazy evaluation stops you updating a products postion when it is initally postion 0 in the category. I have tried to create a bug report for this but the bug tracker is broken. I will create one latter. Search "Cannot Update Position of Product In Category When Start Position = 0 using Webservice" to find.

 

Cheers - Rich

Link to comment
Share on other sites

  • 2 weeks later...
Hi hygap, thanks for sharing your experience ;)

 

I' have the same problem and tried your solution, but unfortunately Its doesn't working for me. I'm on Prestashop 1.6.1

 

I have followed the steps: 

 

1 - Create the product via web service. OK.

2 - Prepare the update process, passing my new value to the position_in_category field, for example 14

3 - Unset the associations->categories node.

4 - Call to the update process

 

And I always get an "1" in the response for the position_in_category field, instead of my value previously passed.

 

I also tried to remove the associations node completely, but with the same result.

 

I'm stuck, and really need to order my products via web service. Can anyone help me?

 

Thank you ;)

Link to comment
Share on other sites

  • 2 months later...
  • 10 months later...

HI All

 

After digging though the Prestashop source I think I have discovered the issue.

 

It seems that if you pass the object back just as it comes out (with just the position_in_category changed) Prestashop does indeed update the category to the correct position. This is achieved in the function setWsPositionInCategory in the main Product class.

 

However after calling this function the webservice gets on to process the contents of <associations><categories> ... </categories></associations> which I am also passing back. This causes function setWsCategories to be called in the main Product class. This lovely method deletes all your category associations for that product and then recreates them again just whacking the position to the end of the category it is added to.

 

So the solution is to unset this in the update which can be achieved by adding the following before passing the product back to be updated.

unset($prestashopProduct->associations->categories);

This stops the webservice from updating all the cats. Just runing setWsPositionInCategory which updates everything fine.

 

 

But just when I thought this was a total waste of my time I have at least spyed a small error in the Prestashop source code. Which due to some lazy evaluation stops you updating a products postion when it is initally postion 0 in the category. I have tried to create a bug report for this but the bug tracker is broken. I will create one latter. Search "Cannot Update Position of Product In Category When Start Position = 0 using Webservice" to find.

 

Cheers - Rich

 

 

Hi all

 

I had same problems with webservices and PS 1.6.1.3. After digging in the repository and the code finally found a (partial) solution:

 

https://github.com/PrestaShop/PrestaShop/pull/5308

 

With this commit you don't have to unset the 'associations' node to perform a position change. Tested and working ok.

 

I said partial solution because there is a bug left that I couldn't solve yet: you can't change the product position when its initially position is 0.

I think the bug is in the SetWSPositionInCategory function inside product.php, but I'm an amateur .NET programmer so it's hard to find the buggy code in php for me.

 

Regards.

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

  • 2 months 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...