Hi everyone,
I am trying to implement custom PLU barcode parsing in Odoo 18 POS using JavaScript patching, but it is not working.
My goal:
Barcode format: 0PPPPWWWWW
First 5 digits → product barcode
Last 5 digits → weight in grams
my code:
{
'name': 'POS PLU Barcode Parser',
'version': '18.0.1.0.0',
'category': 'Point of Sale',
'summary': 'Adds support for parsing weight from PLU barcodes in the Point of Sale.',
'depends': ['point_of_sale'],
'assets': {
'point_of_sale._assets_pos': [
'zl_product_plu_barcode/static/src/js/plu_barcode_parser.js',
],
},
'installable': True,
'auto_install': False,
'license': 'LGPL-3',
}/** @odoo-module **/
import { patch } from "@web/core/utils/patch";
import { ProductScreen } from "@point_of_sale/app/screens/product_screen/product_screen";
import { _t } from "@web/core/l10n/translation";
patch(ProductScreen.prototype, {
/**
* This is the correct function to override for Odoo 18.
* The useBarcodeReader hook in ProductScreen directs product-related scans to this method.
*/
async _barcodeProductAction(code) {
const barcode = code.code;
// Our custom logic for weight-embedded barcodes
if (barcode && barcode.length === 10 && barcode.startsWith('0') && /^\d+$/.test(barcode)) {
const product_barcode = barcode.substring(0, 5).replace(/^0+/, '');
const weight = parseFloat(barcode.substring(5)) / 1000.0;
// Create a mock code object for the product search
const productCode = { code: product_barcode, type: 'product' };
const product = await this._getProductByBarcode(productCode);
if (product) {
// Product found. Call the same function as the original method,
// but inject our custom quantity into the options object.
await this.pos.addLineToCurrentOrder(
{ product_id: product },
{ code, quantity: weight }, // Pass original code and new quantity
product.needToConfigure()
);
this.numberBuffer.reset(); // Reset buffer like the original function
return; // Stop further processing
} else {
// Product not found, show a notification.
this.pos.env.services.notification.add(
_t("Product with barcode '%s' not found.", product_barcode),
{ type: 'danger' }
);
return;
}
}
// If it's not our custom weight barcode, fall back to the original Odoo method.
return await super._barcodeProductAction(code);
}
});
The module installs successfully, but barcode scanning does not trigger my custom logic.
Any help would be greatly appreciated.
Thank you.