如何覆盖 Odoo 15 中的 js 方法?

How to override js method in Odoo 15?

任何人都可以给我最少的代码,以便我可以为 JS 方法覆盖任何方法吗?

changeMode(mode) {
        if (!this.hasPriceControlRights && mode === 'price' ) {
          
            return;
        }
        if (!this.hasManualDiscount && mode === 'discount') {
            return;
        }
        this.trigger('set-numpad-mode', { mode });
    }

这可能对你有帮助,我继承了以下代码的销售点小部件屏幕,

odoo.define('js_frame_work_sample.jsframework', function (require) {
"use strict";

var Screens = require('point_of_sale.screens');
var core = require('web.core');
var _t = core._t;

Screens.PaymentScreenWidget.include({
    order_is_valid: function(force_validation) {
    var self = this;
    var order = this.pos.get_order();

    // FIXME: this check is there because the backend is unable to
    // process empty orders. This is not the right place to fix it.
    var order_paid = order.get_total_paid()
    console.log("order Paid", order_paid)

    if (order_paid <= 30) {
    alert("The sub total is below 30, No purchase")
    return false;
    }

    if (order.get_orderlines().length === 0) {
        this.gui.show_popup('error',{
            'title': _t('Empty Order'),
            'body':  _t('There must be at least one product in your order before it can be validated'),
        });
        return false;
    }

    if (!order.is_paid() || this.invoicing) {
        return false;
    }

    // The exact amount must be paid if there is no cash payment method defined.
    if (Math.abs(order.get_total_with_tax() - order.get_total_paid()) > 0.00001) {
        var cash = false;
        for (var i = 0; i < this.pos.cashregisters.length; i++) {
            cash = cash || (this.pos.cashregisters[i].journal.type === 'cash');
        }
        if (!cash) {
            this.gui.show_popup('error',{
                title: _t('Cannot return change without a cash payment method'),
                body:  _t('There is no cash payment method available in this point of sale to handle the change.\n\n Please pay the exact amount or add a cash payment method in the point of sale configuration'),
            });
            return false;
        }
    }

    // if the change is too large, it's probably an input error, make the user confirm.
    if (!force_validation && order.get_total_with_tax() > 0 && (order.get_total_with_tax() * 1000 < order.get_total_paid())) {
        this.gui.show_popup('confirm',{
            title: _t('Please Confirm Large Amount'),
            body:  _t('Are you sure that the customer wants to  pay') +
                   ' ' +
                   this.format_currency(order.get_total_paid()) +
                   ' ' +
                   _t('for an order of') +
                   ' ' +
                   this.format_currency(order.get_total_with_tax()) +
                   ' ' +
                   _t('? Clicking "Confirm" will validate the payment.'),
            confirm: function() {
                self.validate_order('confirm');
            },
        });
        return false;
    }

    return true;
},


});

});

验证 order_paid 不少于 30 就可以了。

您需要在注册表中扩展 in-place 一个 class,它在销售点 ClassRegistry 中有记录。

例子

odoo.define('MODULE_NAME.NumpadWidget', function(require) {
    'use strict';

    const NumpadWidget = require('point_of_sale.NumpadWidget');
    const Registries = require('point_of_sale.Registries');

    const CustomNumpadWidget = NumpadWidget => class extends NumpadWidget {
        changeMode(mode) {
        
            return super.changeMode(mode);
        }
    };

    Registries.Component.extend(NumpadWidget, CustomNumpadWidget);

    return NumpadWidget;
 });