Jump to content

[WEBSERVICE API][prestapyt] Cannot assign categories to a product PrestaShopWebServiceDict


Recommended Posts

Hello everyone!

I am trying to edit product categories via prestapyt PrestaShopWebServiceDict

The error I'm getting is:
prestapyt.prestapyt.PrestaShopWebServiceError: "Cannot set a value greater than the total number of products in the category."

What I tried to do is that for every category I'm assigning a product to, I add this product ID to ['category']['associations']['products']['product'] and edit via prestashop.edit('categories', cat_ID)
It doesn't throw an error, but after inspecting I see that product association has not been added.
I thought that it was limited by 'nb_products_recursive' and tried to +=1 but it's not writeable, so I cannot increase it in any way.

I've written other functions that are adding products with photos from scratch, editing prices, etc and everything works brilliantly except this category assignment.
I think I've tried everything , I've searched for answers in stack, docummentation, asked chatgpt multiple times but I couldn't solve this issue. I would really appreciate if someone could give me a hint of what can I do.

Here is the most important part of my code that doesn't work:

 

    for product_id in product_ids_list:
        product = prestashop.get('products', product_id)
        
    <<<code that assign categories using openai API and returns a list of correct ID categories>>>
   
    print(cats)
    ['2', '12', '42']

            for c in cats:
                cato = prestashop.get('categories', int(c))
                cato['category']['associations']['products']['product'].append({'id': product['product']['id']})
                cato['category'].pop('level_depth')
                cato['category'].pop('nb_products_recursive')
                prestashop.edit('categories', cato)

        cats_format = [{'id': cat_id} for cat_id in cats]
        product['product']['id_category_default'] = cats[-1]
        product['product']['associations']['categories']['category'] = cats_format

        product['product'].pop('manufacturer_name')
        product['product'].pop('quantity')

        if not product['product']['position_in_category']['value'].isdigit():
            product['product']['position_in_category']['value'] = '1'
        if int(product['product']['position_in_category']['value']) < 1:
            product['product']['position_in_category']['value'] = str(1)

        prestashop.edit('products', product)

 

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