Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
1694 Widoki

Hey, I've got an error, when I create a product variant dynamicly and add it to the cart with python code in an automation action.


This is the error:

Der Vorgang kann nicht ausgeführt werden: duplicate key value violates unique constraint "product_product_combination_unique" DETAIL: Key (product_tmpl_id, combination_indices)=(2208, ) already exists. 

This is how I create th variant:

used_variant = env['product.product'].create({

            'product_tmpl_id':paddle.id,

            'product_template_variant_value_ids':[(6,0,translated_values)]

        })


this is how I add it to the order.line

order_line = env['sale.order.line'].create({

    'order_id': order.id,'product_id': used_variant.id,'product_uom_qty': 1,'price_unit': used_variant.lst_price,

})

Awatar
Odrzuć
Najlepsza odpowiedź

Hi,


The error you are encountering indicates that you are trying to create a product variant with a combination of attributes that already exist. The unique constraint on product_tmpl_id and combination_indices ensures that each combination of attribute values for a product template is unique.


To avoid this error, you should first check if the product variant with the given attribute values already exists before creating a new one. Follow the below to modify your code to handle this:


# Check if the variant already exists

existing_variant = env['product.product'].search([

    ('product_tmpl_id', '=', paddle.id),

    ('product_template_variant_value_ids', 'in', translated_values)

], limit=1)


# Use the existing variant or create a new one

if existing_variant:

    used_variant = existing_variant

else:

    used_variant = env['product.product'].create({

        'product_tmpl_id': paddle.id,

        'product_template_variant_value_ids': [(6, 0, translated_values)]

    })


# Add the variant to the order line

order_line = env['sale.order.line'].create({

    'order_id': order.id,

    'product_id': used_variant.id,

    'product_uom_qty': 1,

    'price_unit': used_variant.lst_price,

})



Hope it helps

Awatar
Odrzuć

I have a similar issue. How you create the variable translated_values for populating the product_template_variant_value_ids filed in the product.product creation method?

Powiązane posty Odpowiedzi Widoki Czynność
0
cze 24
1051
0
cze 24
1145
1
mar 24
954
1
sie 24
1257
4
kwi 24
2847