fix: use WC attribute ID (not name) when setting variation attributes

WooCommerce silently ignores attribute 'name' on variation updates —
requires 'id' (the WC attribute ID). Built a lookup map from the
parent product's wc_attributes and use 'id' in all variation payloads.
Fixed in all 3 files: variant push wizard, product creation wizard,
and product map direct push.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsinghpal
2026-04-01 18:59:54 -04:00
parent 71dea1f91b
commit 75ceee1e69
3 changed files with 38 additions and 16 deletions

View File

@@ -108,6 +108,11 @@ class WooVariantPushWizard(models.TransientModel):
'options': wc_terms,
})
# Build a lookup: Odoo attribute name → WC attribute ID
wc_attr_id_map = {}
for wc_attr in wc_attributes:
wc_attr_id_map[wc_attr['name'].upper()] = wc_attr['id']
# Step 2: Update product as variable with attributes + set default
# Default attribute = first included variant's attribute values
default_attrs = []
@@ -234,13 +239,17 @@ class WooVariantPushWizard(models.TransientModel):
if not wc_var_id:
continue
# Build variation attributes from Odoo
# Build variation attributes with WC attribute IDs
var_attributes = []
for ptav in variant.product_template_attribute_value_ids:
var_attributes.append({
'name': ptav.attribute_id.name,
'option': ptav.name,
})
attr_name = ptav.attribute_id.name
wc_attr_id = wc_attr_id_map.get(attr_name.upper(), 0)
attr_entry = {'option': ptav.name}
if wc_attr_id:
attr_entry['id'] = wc_attr_id
else:
attr_entry['name'] = attr_name
var_attributes.append(attr_entry)
var_data = {
'regular_price': str(line.regular_price),