Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
1697 Visualizzazioni

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,

})

Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona

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?

Post correlati Risposte Visualizzazioni Attività
0
giu 24
1051
0
giu 24
1145
1
mar 24
954
1
ago 24
1258
4
apr 24
2847