Jump to content

[SOLVED]Can't add product to empty category using webservice


tmackay

Recommended Posts

Forgive a newbie for posting out of place, but I couldn't find anything in forum search or existing forge issues.

 

I am trying to import products using the webservice. Just upgraded to 1.6.0.9 to fix a problem with tags not applying and it did, but it introduced a new problem.

 

When creating a new product, and associating it with an existing but empty category, I get the following error regardless of not setting position_in_category or setting it to 0:

<error>
<code><![CDATA[135]]></code>
<message><![CDATA[You cannot set a position greater than the total number of products in the category, minus 1 (position numbering starts at 0).]]></message>
</error>

It seems to come down to the following logic in setWsPositionInCategory in classes/Product.php

/**
* Webservice setter : set virtual field position in category
*
* @return bool
*/
public function setWsPositionInCategory($position)
{
if ($position < 0)
WebserviceRequest::getInstance()->setError(500, Tools::displayError('You cannot set a negative position, the minimum for a position is 0.'), 134);
$result = Db::getInstance()->executeS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int)$this->id_category_default.'
ORDER BY `position`
');
if ($position + 1 > count($result))
WebserviceRequest::getInstance()->setError(500, Tools::displayError('You cannot set a position greater than the total number of products in the category, minus 1 (position numbering starts at 0).'), 135);

($position + 1 > count($result)) can never be false when the category is empty (or if you want to assign something after the last position?). Setting this to "if ($position > count($result))" will allow the product to be added, but now might cause problems changing positions of products already in the category (I guess). It seems the count is done prior to adding the product to the category during the creation process or it never gets added at all due to failing the test.

 

Is this a bug or am I going about something in the wrong way? It seems id_category_default is no longer a required field, but I'd rather set it once when creating the product.

 

Edit: Pull request #2034 merged.

Edited by tmackay (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

Hello,

my problem is that I set di_category_default but the product isn't listed in that, none in the backend none in the fronted (the products is not visible).

 

How do you set the category of each product? How do you set multiple categories?

 

I have the same problem with the supplier field.

 

Edit: I found how to associate categories to product but I still have to found how to associate with supplier

$resources->associations->categories->category->id = category_id;
Edited by AlbertoV (see edit history)
Link to comment
Share on other sites

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...