feat: default variant selector — user picks which variant is the default
Added 'Default' toggle column in variant push wizard. First variant is pre-selected as default. User can change it. The default variant's attribute values are set as WC product's default_attributes using WC attribute IDs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -50,6 +50,7 @@ class WooVariantPushWizard(models.TransientModel):
|
||||
attr_values = ', '.join(
|
||||
variant.product_template_attribute_value_ids.mapped('name')
|
||||
)
|
||||
is_first = len(lines) == 0
|
||||
lines.append((0, 0, {
|
||||
'product_id': variant.id,
|
||||
'variant_name': variant.display_name,
|
||||
@@ -60,6 +61,7 @@ class WooVariantPushWizard(models.TransientModel):
|
||||
'cost_price': variant.standard_price,
|
||||
'image': variant.image_variant_1920 or variant.image_1920 or False,
|
||||
'include': True,
|
||||
'is_default': is_first,
|
||||
'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,
|
||||
@@ -114,15 +116,21 @@ class WooVariantPushWizard(models.TransientModel):
|
||||
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
|
||||
# Use the variant marked as default, or first included
|
||||
default_line = self.line_ids.filtered(lambda l: l.include and l.is_default)[:1]
|
||||
if not default_line:
|
||||
default_line = self.line_ids.filtered('include')[:1]
|
||||
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,
|
||||
})
|
||||
if default_line and default_line.product_id:
|
||||
for ptav in default_line.product_id.product_template_attribute_value_ids:
|
||||
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
|
||||
default_attrs.append(entry)
|
||||
|
||||
parent_update = {
|
||||
'type': 'variable',
|
||||
@@ -323,6 +331,7 @@ class WooVariantPushLine(models.TransientModel):
|
||||
cost_price = fields.Float(string='Cost', digits='Product Price')
|
||||
image = fields.Binary(string='Image', attachment=False)
|
||||
include = fields.Boolean(string='Include', default=True)
|
||||
is_default = fields.Boolean(string='Default')
|
||||
already_synced = fields.Boolean(string='Already Synced')
|
||||
wc_variation_id = fields.Integer(string='WC Variation ID')
|
||||
map_id = fields.Integer(string='Map Record ID')
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
<field name="line_ids">
|
||||
<list editable="bottom">
|
||||
<field name="include" widget="boolean_toggle"/>
|
||||
<field name="is_default" string="Default" widget="boolean_toggle" force_save="1"/>
|
||||
<field name="product_id" column_invisible="1"/>
|
||||
<field name="variant_name" readonly="1" force_save="1"/>
|
||||
<field name="attribute_values" readonly="1" force_save="1"/>
|
||||
|
||||
Reference in New Issue
Block a user