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.
Generate Product Barcodes Automatically
Follow the tutorial below to automatically generate a unique barcode based on product name, type, size, and color.
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})
POLY-TAF-S-W
|
8 Comments
Georgia Reader Reply
Et rerum totam nisi. Molestiae vel quam dolorum vel voluptatem et et. Est ad aut sapiente quis molestiae est qui cum soluta. Vero aut rerum vel. Rerum quos laboriosam placeat ex qui. Sint qui facilis et.
Aron Alvarado Reply
Ipsam tempora sequi voluptatem quis sapiente non. Autem itaque eveniet saepe. Officiis illo ut beatae.
Lynda Small Reply
Enim ipsa eum fugiat fuga repellat. Commodi quo quo dicta. Est ullam aspernatur ut vitae quia mollitia id non. Qui ad quas nostrum rerum sed necessitatibus aut est. Eum officiis sed repellat maxime vero nisi natus. Amet nesciunt nesciunt qui illum omnis est et dolor recusandae. Recusandae sit ad aut impedit et. Ipsa labore dolor impedit et natus in porro aut. Magnam qui cum. Illo similique occaecati nihil modi eligendi. Pariatur distinctio labore omnis incidunt et illum. Expedita et dignissimos distinctio laborum minima fugiat. Libero corporis qui. Nam illo odio beatae enim ducimus. Harum reiciendis error dolorum non autem quisquam vero rerum neque.
Sianna Ramsay Reply
Et dignissimos impedit nulla et quo distinctio ex nemo. Omnis quia dolores cupiditate et. Ut unde qui eligendi sapiente omnis ullam. Placeat porro est commodi est officiis voluptas repellat quisquam possimus. Perferendis id consectetur necessitatibus.
Nolan Davidson Reply
Distinctio nesciunt rerum reprehenderit sed. Iste omnis eius repellendus quia nihil ut accusantium tempore. Nesciunt expedita id dolor exercitationem aspernatur aut quam ut. Voluptatem est accusamus iste at. Non aut et et esse qui sit modi neque. Exercitationem et eos aspernatur. Ea est consequuntur officia beatae ea aut eos soluta. Non qui dolorum voluptatibus et optio veniam. Quam officia sit nostrum dolorem.
Kay Duggan Reply
Dolorem atque aut. Omnis doloremque blanditiis quia eum porro quis ut velit tempore. Cumque sed quia ut maxime. Est ad aut cum. Ut exercitationem non in fugiat.