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:
@@ -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),
|
||||
|
||||
@@ -585,6 +585,9 @@ class WooProductCreateWizard(models.TransientModel):
|
||||
'company_id': inst.company_id.id,
|
||||
})
|
||||
|
||||
# Build WC attribute ID lookup
|
||||
wc_attr_id_map = {a['name'].upper(): a['id'] for a in wc_attributes}
|
||||
|
||||
# Create variations
|
||||
variation_count = 0
|
||||
for line in self.variant_line_ids:
|
||||
@@ -592,13 +595,17 @@ class WooProductCreateWizard(models.TransientModel):
|
||||
continue
|
||||
|
||||
variant = line.product_id
|
||||
# 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(line.sale_price),
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user