fix: populate variant lines in default_get instead of onchange
Onchange wasn't firing when wizard opened via context defaults. Moved all variant line population to default_get so lines are pre-filled immediately. Added debug logging to trace new vs update classification. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -34,42 +34,36 @@ class WooVariantPushWizard(models.TransientModel):
|
||||
res['product_template_id'] = tmpl.id
|
||||
res['product_name'] = tmpl.name
|
||||
res['woo_product_id'] = pm.woo_product_id
|
||||
|
||||
# Populate variant lines
|
||||
lines = []
|
||||
for variant in tmpl.product_variant_ids:
|
||||
already_mapped = self.env['woo.product.map'].search([
|
||||
('instance_id', '=', pm.instance_id.id),
|
||||
('product_id', '=', variant.id),
|
||||
('is_variation', '=', True),
|
||||
], limit=1)
|
||||
|
||||
attr_values = ', '.join(
|
||||
variant.product_template_attribute_value_ids.mapped('name')
|
||||
)
|
||||
lines.append((0, 0, {
|
||||
'product_id': variant.id,
|
||||
'variant_name': variant.display_name,
|
||||
'attribute_values': attr_values,
|
||||
'sku': already_mapped.woo_sku if already_mapped else (variant.default_code or ''),
|
||||
'regular_price': already_mapped.woo_regular_price if already_mapped else variant.list_price,
|
||||
'sale_price': already_mapped.woo_sale_price if already_mapped else 0.0,
|
||||
'cost_price': variant.standard_price,
|
||||
'image': variant.image_variant_1920 or variant.image_1920 or False,
|
||||
'include': True,
|
||||
'already_synced': bool(already_mapped),
|
||||
'wc_variation_id': already_mapped.woo_product_id if already_mapped else 0,
|
||||
'map_id': already_mapped.id if already_mapped else 0,
|
||||
}))
|
||||
res['line_ids'] = lines
|
||||
return res
|
||||
|
||||
@api.onchange('product_map_id')
|
||||
def _onchange_product_map(self):
|
||||
if self.product_map_id and self.product_map_id.product_id:
|
||||
tmpl = self.product_map_id.product_id.product_tmpl_id
|
||||
self.product_template_id = tmpl.id
|
||||
self.product_name = tmpl.name
|
||||
self.woo_product_id = self.product_map_id.woo_product_id
|
||||
|
||||
lines = []
|
||||
for variant in tmpl.product_variant_ids:
|
||||
# Check if already pushed
|
||||
already_mapped = self.env['woo.product.map'].search([
|
||||
('instance_id', '=', self.product_map_id.instance_id.id),
|
||||
('product_id', '=', variant.id),
|
||||
('is_variation', '=', True),
|
||||
], limit=1)
|
||||
|
||||
attr_values = ', '.join(variant.product_template_attribute_value_ids.mapped('name'))
|
||||
lines.append((0, 0, {
|
||||
'product_id': variant.id,
|
||||
'variant_name': variant.display_name,
|
||||
'attribute_values': attr_values,
|
||||
'sku': already_mapped.woo_sku if already_mapped else (variant.default_code or ''),
|
||||
'regular_price': already_mapped.woo_regular_price if already_mapped else variant.list_price,
|
||||
'sale_price': already_mapped.woo_sale_price if already_mapped else 0.0,
|
||||
'cost_price': variant.standard_price,
|
||||
'image': variant.image_variant_1920 or variant.image_1920 or False,
|
||||
'include': True,
|
||||
'already_synced': bool(already_mapped),
|
||||
'wc_variation_id': already_mapped.woo_product_id if already_mapped else 0,
|
||||
'map_id': already_mapped.id if already_mapped else False,
|
||||
}))
|
||||
self.line_ids = lines
|
||||
|
||||
def action_push(self):
|
||||
"""Push selected variants to WooCommerce."""
|
||||
self.ensure_one()
|
||||
@@ -78,8 +72,14 @@ class WooVariantPushWizard(models.TransientModel):
|
||||
client = inst._get_client()
|
||||
tmpl = self.product_template_id
|
||||
|
||||
_logger.info(
|
||||
"Variant push wizard: %d lines total, fields: %s",
|
||||
len(self.line_ids),
|
||||
[(l.variant_name, l.already_synced, l.wc_variation_id, l.map_id) for l in self.line_ids],
|
||||
)
|
||||
lines_new = self.line_ids.filtered(lambda l: l.include and not l.already_synced)
|
||||
lines_update = self.line_ids.filtered(lambda l: l.include and l.already_synced)
|
||||
_logger.info("Variant push: %d new, %d update", len(lines_new), len(lines_update))
|
||||
|
||||
if not lines_new and not lines_update:
|
||||
raise UserError("No variants selected.")
|
||||
|
||||
Reference in New Issue
Block a user