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

@@ -207,10 +207,12 @@ class WooProductMap(models.Model):
except Exception as e:
raise UserError("Failed to convert WC product to variable: %s" % str(e))
# Build WC attribute ID lookup
wc_attr_id_map = {a['name'].upper(): a['id'] for a in wc_attributes}
# Step 3: Create a WC variation for each Odoo variant
created = 0
for variant in variants:
# Check if variation already mapped
existing = self.search([
('instance_id', '=', inst.id),
('product_id', '=', variant.id),
@@ -219,13 +221,17 @@ class WooProductMap(models.Model):
if existing:
continue
# Build variation attributes
# Build variation attributes with WC 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_aid = wc_attr_id_map.get(attr_name.upper(), 0)
entry = {'option': ptav.name}
if wc_aid:
entry['id'] = wc_aid
else:
entry['name'] = attr_name
var_attributes.append(entry)
var_data = {
'regular_price': str(variant.list_price),