ivan.siregar@gmail.com +6281809456734

Image source: Gelato

Product Variant-based Barcode Generator

Managing product variants in Odoo can be easier when each variant has a meaningful barcode. Instead of assigning random barcode numbers, we can automatically generate a barcode using the product name, type, size, and color.

For example: Polyester – Taffeta – Small – Red
Can generate: POLY-TAF-S-R

The barcode follows this format: 4 Characters – 3 Characters – 1 Character – 1 Character
Where, POLY = Product Name; TAF = Type; S = Size; R = Color

This video will help you understand, try, and manage the process step by step. The full code is provided below, so feel free to copy and paste it for your own use.

🎥 VIDEO TUTORIAL

Generate Product Barcodes Automatically

Follow the tutorial below to automatically generate a unique barcode based on product name, type, size, and color.

▶ Watch Tutorial
Python
if record:
                    # 1. Generate the 4-character Name prefix
                    # Grabs the parent template name (e.g., "Polyester, Greige")
                    template_name = record.product_tmpl_id.name or ""
                    
                    if "polyester" in template_name.lower():
                        name_code = "POLY"
                    else:
                        # Fallback: takes first 4 letters if it's a different product name
                        name_code = template_name[:4].replace(" ", "").replace(",", "").upper()
                
                    # 2. Extract variant attributes
                    type_code = ""
                    size_code = ""
                    color_code = ""
                    
                    for ptav in record.product_template_attribute_value_ids:
                        attr_name = ptav.attribute_id.name.lower()
                        val_name = ptav.name.strip()
                        
                        # Type Attribute (e.g., Taffeta -> TAF)
                        if attr_name == 'type':
                            type_code = val_name[:3].upper()
                            
                        # Size Attribute (e.g., Small -> S, Medium -> M, Large -> L)
                        elif attr_name == 'size':
                            size_code = val_name[0].upper()
                            
                        # Color Attribute (e.g., Blue -> B, Green -> G, White -> W)
                        elif attr_name == 'color':
                            color_code = val_name[0].upper()
                            
                    # 3. Combine fields into the barcode format: POLY-TAF-S-W
                    if type_code and size_code and color_code:
                        generated_barcode = f"{name_code}-{type_code}-{size_code}-{color_code}"
                        record.write({'barcode': generated_barcode})
Example Result POLY-TAF-S-W
Ready to transform
your textile business with Odoo ?
From yarn to fabric, dyeing to finishing, and production to delivery, textile manufacturing demands more than a standard ERP. With deep expertise in Odoo for textile manufacturing, we help businesses model their processes, streamline production, configure Odoo to fit their operations, and build the right customizations where needed.
  Complex textile processes?
We know how to make Odoo works for them.
Let’s build it, just contact us!
(+62) 81809456734
www.bandung-odoo.com