fix: variant update now sets attribute values and default variant on WC

- Update path sends attributes per variation (was missing, causing
  "Any COLOR..." on WC)
- Sets default_attributes on parent product (first variant's values)
- Better API error logging with response body

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsinghpal
2026-04-01 18:20:52 -04:00
parent 8983c8bd50
commit 39d4fe5020
2 changed files with 34 additions and 6 deletions

View File

@@ -39,6 +39,11 @@ class WooApiClient:
params=params,
timeout=self.timeout,
)
if response.status_code >= 400:
_logger.error(
"WC API %s %s returned %s: %s",
method, endpoint, response.status_code, response.text[:500],
)
response.raise_for_status()
return response.json()
except Exception as exc:

View File

@@ -105,15 +105,29 @@ class WooVariantPushWizard(models.TransientModel):
'options': wc_terms,
})
# Step 2: Convert product to variable with attributes
# Step 2: Update product as variable with attributes + set default
# Default attribute = first included variant's attribute values
default_attrs = []
first_included = self.line_ids.filtered('include')[:1]
if first_included and first_included.product_id:
for ptav in first_included.product_id.product_template_attribute_value_ids:
default_attrs.append({
'name': ptav.attribute_id.name,
'option': ptav.name,
})
parent_update = {
'type': 'variable',
'attributes': wc_attributes,
}
if default_attrs:
parent_update['default_attributes'] = default_attrs
try:
client.update_product(pm.woo_product_id, {
'type': 'variable',
'attributes': wc_attributes,
})
client.update_product(pm.woo_product_id, parent_update)
pm.woo_product_type = 'variable'
except Exception as e:
raise UserError("Failed to convert WC product to variable: %s" % str(e))
raise UserError("Failed to update WC product: %s" % str(e))
# Step 3: Create NEW variations
created = 0
@@ -217,9 +231,18 @@ class WooVariantPushWizard(models.TransientModel):
if not wc_var_id:
continue
# Build variation attributes from Odoo
var_attributes = []
for ptav in variant.product_template_attribute_value_ids:
var_attributes.append({
'name': ptav.attribute_id.name,
'option': ptav.name,
})
var_data = {
'regular_price': str(line.regular_price),
'sku': line.sku or '',
'attributes': var_attributes,
'manage_stock': True,
'stock_quantity': int(variant.qty_available),
}