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

自定义ODOO 15 POS收据:JS中获取pos.order新增字段问题

解决ODOO 15 POS收据显示pos.order新增字段的问题

1. 后端配置:确保字段传递到POS前端

首先要让POS加载时同步新增的pos.order字段,创建模块继承对应模型并修改加载参数:

from odoo import models, fields

class PosOrder(models.Model):
    _inherit = 'pos.order'

    # 替换为你的实际新增字段,示例为自定义字符字段
    custom_field = fields.Char(string="自定义字段")

class PosSession(models.Model):
    _inherit = 'pos.session'

    def _loader_params_pos_order(self):
        params = super()._loader_params_pos_order()
        # 将新增字段加入POS加载的字段列表
        params['search_params']['fields'].append('custom_field')
        return params

这段代码的作用:

  • 给pos.order模型添加自定义字段
  • 修改POS会话加载订单数据的规则,确保新增字段被同步到前端

2. 前端JS:扩展POS Order模型

通过JS扩展POS的Order模型,让get_order()能读取到新增字段:

odoo.define('your_module_name.pos_order_extension', function(require) {
    "use strict";

    const models = require('point_of_sale.models');
    const Order = models.Order;

    // 继承原有Order模型
    models.Order = models.Order.extend({
        initialize: function(attr, options) {
            this.custom_field = attr.custom_field || '';
            this._super(attr, options);
        },

        export_as_JSON: function() {
            var json = this._super(...arguments);
            json.custom_field = this.custom_field;
            return json;
        },

        init_from_JSON: function(json) {
            this._super(json);
            this.custom_field = json.custom_field;
        },
    });
});

这段代码会把新增字段整合进POS的Order模型逻辑,确保前端能正确读取、存储该字段值。

3. 修改收据模板显示字段

继承POS默认收据模板,添加字段展示:

<odoo>
    <template id="pos_receipt_custom_field" inherit_id="point_of_sale.pos_receipt">
        <xpath expr="//div[@class='pos-receipt-order']" position="inside">
            <!-- 自定义字段显示区域,可根据需求调整位置 -->
            <div class="row">
                <div class="col-6">自定义字段:</div>
                <div class="col-6 text-right"><t t-esc="order.custom_field"/></div>
            </div>
        </xpath>
    </template>
</odoo>

关键注意事项

  • 模块__manifest__.py中要添加依赖:'depends': ['point_of_sale']
  • 重启ODOO服务并升级模块
  • 刷新POS前端页面(F5),确保新代码和模板生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 05:25:01