Skip to content

Commit

Permalink
[IMP] Add action for choosing a variant as the price reference
Browse files Browse the repository at this point in the history
- This can be very handy for the website if we want to use not the cheapest variant as the default
  displayed price. It also reorders the attribute values such that when opening a product on the
  website, it immediately shows the reference variant.
  • Loading branch information
ecino committed Jun 20, 2023
1 parent 943ffb7 commit 39c984c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion product_variant_sale_price/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"license": "AGPL-3",
"installable": True,
"depends": ["account", "sale"],
"data": ["views/product_views.xml"],
"data": ["data/server_actions.xml", "views/product_views.xml"],
"post_init_hook": "set_sale_price_on_variant",
}
17 changes: 17 additions & 0 deletions product_variant_sale_price/data/server_actions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<odoo>
<record model="ir.actions.server" id="set_price_reference">
<field name="name">Set price reference</field>
<field name="model_id" ref="model_product_product" />
<field name="state">code</field>
<field name="binding_model_id" ref="model_product_product" />
<field name="binding_type">action</field>
<field name="code">
if len(records) > len(records.mapped("product_tmpl_id")):
raise UserError("You can only set one variant as the price reference of a product.")
for r in records.with_context(skip_update_fix_price=True):
r.product_tmpl_id.write({"list_price": r.fix_price})
new_seq = min(r.product_tmpl_id.attribute_line_ids.mapped("value_ids.sequence") or [r.fix_price])
r.mapped("product_template_variant_value_ids.product_attribute_value_id").write({"sequence": new_seq - 1})
</field>
</record>
</odoo>
28 changes: 21 additions & 7 deletions product_variant_sale_price/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@ def write(self, vals):
template._update_fix_price(vals)
return res

def _get_combination_info(self, combination=False, product_id=False, add_qty=1, pricelist=False,
parent_combination=False, only_template=False):
res = super()._get_combination_info(combination, product_id, add_qty, pricelist,
parent_combination, only_template)
res['price_extra'] = 0.0
def _get_combination_info(
self,
combination=False,
product_id=False,
add_qty=1,
pricelist=False,
parent_combination=False,
only_template=False,
):
res = super()._get_combination_info(
combination,
product_id,
add_qty,
pricelist,
parent_combination,
only_template,
)
res["price_extra"] = 0.0
return res


Expand Down Expand Up @@ -88,7 +101,8 @@ def _inverse_product_lst_price(self):
product.write(vals)

def _compute_product_price_extra(self):
"""the sale.order.line module calculates the price_unit by adding the value of price_extra and this can
generate inconsistencies if the field has old data stored."""
"""the sale.order.line module calculates the price_unit by adding
the value of price_extra and this can generate inconsistencies
if the field has old data stored."""
for product in self:
product.price_extra = 0.0

0 comments on commit 39c984c

Please sign in to comment.