Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
1701 Переглядів

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,

})

Аватар
Відмінити
Найкраща відповідь

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

Аватар
Відмінити

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?

Related Posts Відповіді Переглядів Дія
0
черв. 24
1054
0
черв. 24
1149
1
бер. 24
962
1
серп. 24
1263
4
квіт. 24
2852