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:
@@ -39,6 +39,11 @@ class WooApiClient:
|
|||||||
params=params,
|
params=params,
|
||||||
timeout=self.timeout,
|
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()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|||||||
@@ -105,15 +105,29 @@ class WooVariantPushWizard(models.TransientModel):
|
|||||||
'options': wc_terms,
|
'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:
|
try:
|
||||||
client.update_product(pm.woo_product_id, {
|
client.update_product(pm.woo_product_id, parent_update)
|
||||||
'type': 'variable',
|
|
||||||
'attributes': wc_attributes,
|
|
||||||
})
|
|
||||||
pm.woo_product_type = 'variable'
|
pm.woo_product_type = 'variable'
|
||||||
except Exception as e:
|
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
|
# Step 3: Create NEW variations
|
||||||
created = 0
|
created = 0
|
||||||
@@ -217,9 +231,18 @@ class WooVariantPushWizard(models.TransientModel):
|
|||||||
if not wc_var_id:
|
if not wc_var_id:
|
||||||
continue
|
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 = {
|
var_data = {
|
||||||
'regular_price': str(line.regular_price),
|
'regular_price': str(line.regular_price),
|
||||||
'sku': line.sku or '',
|
'sku': line.sku or '',
|
||||||
|
'attributes': var_attributes,
|
||||||
'manage_stock': True,
|
'manage_stock': True,
|
||||||
'stock_quantity': int(variant.qty_available),
|
'stock_quantity': int(variant.qty_available),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user