You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 17 POS中如何在models.js的set_discount获取产品最大折扣

Odoo 17 POS 实现产品最大折扣校验步骤

1. 后端添加产品最大折扣字段

首先需要在product.product模型中新增最大折扣字段,用于后台配置每个产品的折扣上限。创建或修改自定义模块的Python文件:

from odoo import models, fields

class ProductProduct(models.Model):
    _inherit = 'product.product'

    max_discount = fields.Float(
        string='最大折扣(%)',
        default=100,
        help='设置该产品允许使用的最高折扣百分比'
    )

同时,在产品表单视图中添加这个字段,方便运营人员在后台设置具体数值。

2. 将字段同步到POS前端

要让POS前端能获取到这个字段,需要确保POS加载产品数据时包含max_discount,可通过以下方式实现:

  • 后端关联POS产品字段:继承pos.product模型,关联产品模板的最大折扣字段
    class PosProduct(models.Model):
        _inherit = 'pos.product'
    
        max_discount = fields.Float(
            related='product_tmpl_id.max_discount',
            string='最大折扣(%)'
        )
    
  • 前端扩展产品模型:在POS的JavaScript代码中扩展产品模型,确保字段被绑定到前端对象
    odoo.define('your_custom_module.product', function(require) {
        'use strict';
    
        const Product = require('point_of_sale.Product');
        const Registries = require('point_of_sale.Registries');
    
        const CustomProduct = Product => class extends Product {
            setup() {
                super.setup();
                this.max_discount = this.product.max_discount;
            }
        };
    
        Registries.Model.extend(Product, CustomProduct);
    
        return CustomProduct;
    });
    

3. 在set_discount函数中添加校验逻辑

现在可以直接在目标函数中获取产品的最大折扣值,完善校验逻辑:

set_discount(discount) {
    var parsed_discount =
        typeof discount === "number"
            ? discount
            : isNaN(parseFloat(discount))
            ? 0
            : oParseFloat("" + discount);
    var disc = Math.min(Math.max(parsed_discount || 0, 0), 100);
    
    // 获取最大折扣,默认100%防止未配置的情况
    var maxDiscount = this.product.max_discount || 100;
    if(disc > maxDiscount){
        alert(`折扣不能超过${maxDiscount}%`);
        return;
    }

    this.discount = disc;
    this.discountStr = "" + disc;
}

内容的提问来源于stack exchange,提问作者Ragab Mahmoud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 20:22:18