Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
3006 Näkymät

this is the original code :

/** @odoo-module **/

import { _t } from "@web/core/l10n/translation";
import { ProductScreen } from "@point_of_sale/app/screens/product_screen/product_screen";
import { useService } from "@web/core/utils/hooks";
import { NumberPopup } from "@point_of_sale/app/utils/input_popups/number_popup";
import { ErrorPopup } from "@point_of_sale/app/errors/popups/error_popup";
import { Component } from "@odoo/owl";
import { usePos } from "@point_of_sale/app/store/pos_hook";

export class DiscountButton extends Component {
static template = "pos_discount.DiscountButton";

setup() {
this.pos = usePos();
this.popup = useService("popup");
}
async click() {
var self = this;
const { confirmed, payload } = await this.popup.add(NumberPopup, {
title: _t("Discount Percentage"),
startingValue: this.pos.config.discount_pc,
isInputSelected: true,
});
if (confirmed) {
const val = Math.max(0, Math.min(100, parseFloat(payload)));
await self.apply_discount(val);
}
}

async apply_discount(pc) {
// here i want to add som validation
}
}

ProductScreen.addControlButton({
component: DiscountButton,
condition: function () {
const { module_pos_discount, discount_product_id } = this.pos.config;
return module_pos_discount && discount_product_id;
},
});



and this is my code 

/** @odoo-module **/

const _t = require("@web/core/l10n/translation")._t;
const ProductScreen = require("@point_of_sale/app/screens/product_screen/product_screen").ProductScreen;
const NumberPopup = require("@point_of_sale/app/utils/input_popups/number_popup").NumberPopup;
const ErrorPopup = require("@point_of_sale/app/errors/popups/error_popup").ErrorPopup;
const Component = require("@odoo/owl").Component;
const usePos = require("@point_of_sale/app/store/pos_hook").usePos;
const DiscountButton = require("@pos_discount/overrides/components/discount_button/discount_button").DiscountButton;

export class CustomDiscountButton extends DiscountButton {
async apply_discount(pc) {

console.log('some validation');

await super.apply_discount(pc);


}

async click() {
var self = this;
const { confirmed, payload } = await this.popup.add(NumberPopup, {
title: _t("Discount Percentage"),
startingValue: this.pos.config.discount_pc,
isInputSelected: true,
});
if (confirmed) {
const val = Math.max(0, Math.min(100, parseFloat(payload)));
await self.apply_discount(val);
}
}
}

ProductScreen.addControlButton({
component: CustomDiscountButton,
condition: function () {
const { module_pos_discount, discount_product_id } = this.pos.config;
return module_pos_discount && discount_product_id;
},
});

 

Avatar
Hylkää
Tekijä

/** @odoo-module */

import { DiscountButton } from "@pos_discount/overrides/components/discount_button/discount_button";
import { ErrorPopup } from "@point_of_sale/app/errors/popups/error_popup";
import { patch } from "@web/core/utils/patch";
import { _t } from "@web/core/l10n/translation";

patch(DiscountButton.prototype, {

async apply_discount(pc) {
console.log("test")
await super.apply_discount(arguments);
},
});

should this code work ?

Paras vastaus

Hi,

Try this:

/** @odoo-module **/


import { DiscountButton } from "@pos_discount/overrides/components/discount_button/discount_button";

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


patch(DiscountButton.prototype, {

    setup() {

        super.setup(...arguments);

    },

       async apply_discount(pc) {

                       await super.apply_discount(pc);

       }

});


Hope it helps

Avatar
Hylkää

That works. But I was trying to import it like this and it was throwing errors "unmet dependencies". Why it is not working like this?
```
/** @odoo-module **/

import { registry } from "@web/core/registry";
import { DiscountButton } from "pos_discount/overrides/components/discount_button/discount_button"; // Correct path

// Create a new class that extends the original DiscountButton
export class CustomDiscountButton extends OriginalDiscountButton {
// Override the click method
async click() {
console.log("Custom click function invoked!");

const { confirmed, payload } = await this.popup.add(NumberPopup, {
title: _t("Discount Amount"),
startingValue: 0,
isInputSelected: true,
});
if (confirmed) {
const val = Math.max(0, parseFloat(payload));
await this.apply_discount(val);
}
}
}

// Register your custom button to replace the original one
registry.category("control_buttons").add("DiscountButton", CustomDiscountButton);

```

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
syysk. 23
4673
0
heinäk. 17
2735
0
maalisk. 15
6240
0
huhtik. 25
1228
1
elok. 24
2573