跳至内容
菜单
此问题已终结
3 回复
4206 查看

Can anyone give me minimal code so that any method I can override for JS method? in pos odoo




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


形象
丢弃
最佳答案

Working code to override the changeMode(mode) method in Odoo 15 POS:

odoo.define('your_module.NumpadWidgetPatch', function (require) {
    'use strict';

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

    NumpadWidget.include({
        changeMode: function (mode) {
            console.log("Overridden changeMode:", mode);

            if (!this.hasPriceControlRights && mode === 'price') {
                console.log('No price rights!');
                return;
            }
            if (!this.hasManualDiscount && mode === 'discount') {
                console.log('No discount rights!');
                return;
            }

            this.trigger('set-numpad-mode', { mode });
        },
    });
});

Add to your module assets:

In your module’s __manifest__.py:

'assets': {
    'point_of_sale.assets': [
        'your_module/static/src/js/numpad_patch.js',
    ],
},


形象
丢弃
最佳答案

Hi,

In Odoo 15, you can override a JavaScript method by creating a custom module and using the patch utility from OWL. First, include your custom JS file in the point_of_sale.assets bundle in your module’s __manifest__.py. Inside the JS file, import the component you want to override (e.g., Numpad in POS) and use patch to extend its prototype. For example, to override the changeMode method, write:


/** @odoo-module **/

import { patch } from "@web/core/utils/patch";

import { Numpad } from "@point_of_sale/app/screens/numpad/numpad";


patch(Numpad.prototype, "my_pos_custom_numpad_patch", {

    changeMode(mode) {

        console.log("Custom logic for changeMode", mode);

        this.trigger('set-numpad-mode', { mode });

    },

});


After restarting Odoo and upgrading the module, your custom logic will replace or extend the original method. This is the clean and recommended way to override JS methods in Odoo 15.


Hope it helps


形象
丢弃
最佳答案

Inherits the class of the method,Rewriting method

形象
丢弃
相关帖文 回复 查看 活动
2
5月 22
14968
2
4月 22
5160
1
7月 25
4559
10
12月 24
33524
1
11月 23
10998