Enable Dark Mode!
how-to-add-an-icon-in-the-barcode-interface-in-odoo-18.jpg
By: Nihala KP

How to Add an Icon in the Barcode Interface in Odoo 18

Technical Odoo 19 Owl

The Barcode App in Odoo connects Odoo Warehouse and makes the workflow smooth.

In this blog, we can understand how to add an icon to the barcode screen using Odoo’s bus service. This is useful in cases where we can show pending orders, unassigned sale orders, and any other specific business requirements.

Step 1: Extend the Barcode Template

To start with, we can inherit the Barcode application’s main component

<templates>
    <t t-inherit="stock_barcode.MainComponent" t-inherit-mode="extension">
        <xpath expr="//div[hasclass('o_barcode_client_action')]" position="inside">
            <div style="
                position:absolute;
                top:20px;
                right:100px;
                background:red;
                color:white;
                padding:4px 8px;
                border-radius:10px;
                font-size:14px;
                z-index:1000;
            ">
                ?? <t t-esc="salesCounter ? salesCounter.count : 0"/>
            </div>
        </xpath>
    </t>
</templates>

The above code inherits the existing barcode interface and adds a small counter badge inside the barcode screen.

The screenshot below shows the icon we created in the interface

How to Add an Icon in the Barcode Interface in Odoo 18-cybrosys

Step 2: Add the JavaScript Logic

The code below shows the JS file to change the counter value.

/** @odoo-module **/
import MainComponent from "@stock_barcode/components/main";
import { patch } from "@web/core/utils/patch";
import { useService } from "@web/core/utils/hooks";
import { useState, onWillStart, onWillUnmount } from "@odoo/owl";
patch(MainComponent.prototype, {
    setup() {
        super.setup();
        this.orm = useService("orm");
        this.busService = this.env.services.bus_service;
        this.salesCounter = useState({ count: 0 });
        this.isMounted = true;
        onWillUnmount(() => {
            this.isMounted = false;
        });
        this.busService.addChannel("sales_counter");
        this.busService.subscribe(
            "update_counter",
            this._onCounterUpdate.bind(this)
        );
        onWillStart(async () => {
            await this._loadCount();
        });
    },
    async _loadCount() {
        try {
            const count = await this.orm.call(
                "sale.order",
                "broadcast_counter",
                []
            );
            if (this.isMounted) {
                this.salesCounter.count = count;
            }
        } catch (err) {
            console.error("Barcode load error:", err);
        }
    },
    _onCounterUpdate(payload) {
        if (!this.isMounted) return;
        if (payload?.count !== undefined) {
            this.salesCounter.count = payload.count;
        }
    },
});

The above code patches the barcode application’s owl main component. When the barcode screen loads, it fetches the current value, store and displays it in the counter without refreshing the page.

Step 3: Add Assets to the Manifest

Include the XML and JavaScript files in your module assets.

'assets': {
    'web.assets_backend': [
        'your_module/static/src/js/barcode_counter.js',
        'your_module/static/src/xml/barcode_counter.xml',
    ],
},

Refresh your models to see the changes.

Customizing the Barcode interface in Odoo 18 is straightforward with OWL components and template inheritance. By adding a small counter badge and connecting it to Odoo's bus service, you can display real-time information directly on the Barcode screen and improve the overall user experience.

To read more about How to Add an Icon in Systray in Odoo 17, refer to our blog How to Add an Icon in Systray in Odoo 17.


Frequently Asked Questions

Can I add more than one icon to the Barcode interface?

Yes, you can add multiple icons by creating additional JS for each icon

In which asset bundle should we add the files?

We can add to “web.assets_backend” bundle

Why do we use the Bus Service in this code?

The bus service is used for real-time communication. The changes appear automatically without refreshing the page.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



WhatsApp