[ADD] user friendly error handling for restricted qty on ecommerce app
Some checks failed
pre-commit / pre-commit (pull_request) Has been cancelled

This commit is contained in:
2026-08-18 17:52:11 +02:00
parent a140edb60e
commit 5d8293f73e
10 changed files with 393 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
from . import product_restricted_qty_mixin
from . import sale_order_line

View File

@@ -0,0 +1,59 @@
# Copyright 2026 Élabore (https://elabore.coop)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, models
from odoo.tools import float_is_zero
from odoo.tools.misc import formatLang
class ProductRestrictedQtyMixin(models.AbstractModel):
"""Extending the OCA abstract mixin makes these helpers available at once
on product.product, product.template and product.category."""
_inherit = "product.restricted.qty.mixin"
def _format_restricted_qty(self, qty):
"""Short form: 6 rather than 6.0, and a locale-aware separator
for decimals."""
if float_is_zero(qty - int(qty), precision_digits=3):
return str(int(qty))
return formatLang(self.env, qty)
def _get_restricted_qty_hint(self):
"""Sentence shown to the customer. Empty when nothing restricts him.
OCA semantics: `force_sale_min_qty` / `force_sale_max_qty` set to Yes
make the bound purely indicative -- the constraint does not block, so
we announce nothing rather than promising a rule that does not exist.
"""
self.ensure_one()
uom = self.uom_id.name if "uom_id" in self._fields else ""
parts = []
if self.sale_min_qty and not self.force_sale_min_qty:
parts.append(
_(
"minimum order: %(qty)s %(uom)s",
qty=self._format_restricted_qty(self.sale_min_qty),
uom=uom,
)
)
if self.sale_multiple_qty:
parts.append(
_(
"in multiples of %(qty)s %(uom)s",
qty=self._format_restricted_qty(self.sale_multiple_qty),
uom=uom,
)
)
if self.sale_max_qty and not self.force_sale_max_qty:
parts.append(
_(
"maximum: %(qty)s %(uom)s",
qty=self._format_restricted_qty(self.sale_max_qty),
uom=uom,
)
)
if not parts:
return ""
hint = " · ".join(parts)
return hint[0].upper() + hint[1:]

View File

@@ -0,0 +1,62 @@
from odoo import _, api, models
from odoo.exceptions import ValidationError
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
@api.constrains(
"product_uom_qty", "sale_min_qty", "sale_max_qty", "sale_multiple_qty"
)
def check_constraint_restricted_qty(self):
"""Rewrite the error message raised by `sale_restricted_qty`.
The upstream message is written for a salesperson: it dumps the
offending products and explains how to tick "force min quantity" on
the product form. Unreadable for an eCommerce customer.
Detection is left to the OCA module -- the `is_qty_*` fields stay the
single source of truth. Only the wording is replaced, so an upstream
change to the rules is followed without breaking anything.
"""
try:
return super().check_constraint_restricted_qty()
except ValidationError:
raise ValidationError(self._get_restricted_qty_error_message()) from None
def _get_restricted_qty_error_message(self):
"""Customer-facing message, one line per unmet restriction."""
details = []
for line in self:
product = line.product_id
if not product.force_sale_min_qty and line.is_qty_less_min_qty:
details.append(
_(
"%(product)s: minimum order quantity is %(qty)s.",
product=product.display_name,
qty=product._format_restricted_qty(line.sale_min_qty),
)
)
if not product.force_sale_max_qty and line.is_qty_bigger_max_qty:
details.append(
_(
"%(product)s: maximum order quantity is %(qty)s.",
product=product.display_name,
qty=product._format_restricted_qty(line.sale_max_qty),
)
)
if line.is_qty_not_multiple_qty:
details.append(
_(
"%(product)s: must be ordered in multiples of %(qty)s.",
product=product.display_name,
qty=product._format_restricted_qty(line.sale_multiple_qty),
)
)
return _(
"The selected quantity is not available for sale:\n\n"
"%s\n\n"
"Please adjust the quantity.",
"\n".join(details),
)