I have created new fields Product Code
and Product Barcode
for using when Product Variant
is created.I have created this fields in both product.template
and product.product
and then when a product is created in Product Template
with Product Code
and Product Barcode
, I want to create a Sequenced Product Code
and Product Barcode
for Product Variants
which will come under product.product
.
Eg:If we give abc
as Product Code
in Product Template
and it have 3 variants, Product Code
in Product Template
should come as abc-1
,abc-2,abc-3
respectively.
Data passing and all is done but in my logic if we delete a variant and add another variant there is a chance it will come in same name.
Eg: If we delete variant abc-1
and add another variant , its Product Code
will come as abc-3
(which is already available)
Code
for variant_ids in to_create_variants:
if self.product_variant_count == 0 :
count = count + 1
else :
count = self.product_variant_count + 1
new_variant = Product.create({
'product_tmpl_id': tmpl_id.id,
'attribute_value_ids': [(6, 0, variant_ids.ids)],
'xn_product_code' : str(tmpl_id.xn_product_code_tmpl) + "-" + str(count),
'xn_product_barcode' : str(tmpl_id.xn_product_barcode_tmpl) + "-" + str(count)
})
I solved this by adding an extra count field to the Product Template