Enable Dark Mode!
how-to-patch-existing-owl-components-in-odoo-18.jpg
By: Swaraj R

How to Patch Existing OWL Components in Odoo 18

Technical Odoo 18

Odoo 18 uses the OWL framework to create web interfaces for various modules. At times, you might need to adjust the behavior of an existing OWL component without completely replacing it. This can be done by leveraging the 'patch' method provided by the OWL framework.

What is Patching?

Patching allows you to alter the behavior of an existing method without fully replacing it. In OWL, this can be done using the framework's 'patch' method, which requires two arguments.

Which class or object requires patching:

* The class or object that needs modification

An object containing the newly added methods or properties

Patching an Order Widget Component

Let's update the 'OrderWidget' component, responsible for the order summary, to include the total number of items in the order lines within the Odoo 18 Point of Sale (POS) order summary.

Before patching the 'OrderWidget' component, we need to import it.

import { OrderWidget } from "@point_of_sale/app/generic_components/order_widget/order_widget";

Also, we need to import the patch

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

Next, we need to extend the view to display the total count in the order summary.

<t t-name="pos_item_counter.OrderWidget"
  t-inherit="point_of_sale.OrderWidget" t-inherit-mode="extension"
  owl="1">
   <xpath expr="//div[hasclass('order-summary')]" position="inside">
       <div class="line">
           <div class="subentry fs-6 text-muted">
               Total Quantity:
               <span class="value">
                   <t t-esc="TotalQuantity"/>
               </span>
           </div>
       </div>
   </xpath>
</t>

1. <t t-name="pos_item_counter.OrderWidget": This line creates a new XML template called "pos_item_counter.OrderWidget." 

2. "t-inherit="point_of_sale.OrderWidget": The attribute t-inherit="point_of_sale.OrderWidget"indicates that this template is adopting the features of an existing template named "point_of_sale.OrderWidget."

3. t-inherit-mode="extension":  This property ensures that the new template will retain the functionality of the existing template rather than completely replacing it.

4. owl="1": Specify that this is an Owl template.

5. <xpath expr="//div[hasclass('order-summary')]" position=" inside">: This section should specify where the changes are to be made. The position="inside" attribute indicates that the updates should be placed within a <div> element with the class "order-summary”.

6. Total Quantity: This text serves as a label, clarifying the significance of the value that comes after it.

7. <span class="value">: This generates a <span> element with the "value" CSS class to display the "Total Quantity" value.

8. <t t-esc="TotalQuantity"/>:This section dynamically inserts the value of 'TotalQuantity' into the HTML. 'TotalQuantity' is a function defined within the OrderWidget component. This code will render the 'TotalQuantity' value in the HTML output, which is likely a variable or data retrieved from the system.

Next, let's patch the OrderWidget component and add a function to calculate the total count of the product quantities in the order line.

/** @odoo-module */
import { OrderWidget } from "@point_of_sale/app/generic_components/order_widget/order_widget";
import { patch } from "@web/core/utils/patch";
patch(OrderWidget.prototype, {
   get TotalQuantity(){
       var totalQuantity = 0;
       this.props.lines.forEach(line => totalQuantity += line.qty);
       return totalQuantity
   }
});

This code introduces a getter method called TotalQuantity to the OrderWidget prototype by extending it with the patch function. The method sums the quantities of each line stored in the props.lines array to calculate the total number of items in the order. It then returns this total, making it simple to display the total quantity in the user interface.

Then the view after adding a product,

How to Patch Existing OWL Components in Odoo 18 cybrosys

The total quantity of items in the POS order is now shown in the order summary.

Patching is a powerful technique for modifying the behavior of third-party programs without disrupting their functionality. In this blog post, we explored an example of patching the OrderWidget in the POS class of the Odoo web framework.

By following the instructions provided here, you can safely apply patches to the Odoo web framework and customize them to suit your specific needs.

Discover how to patch an existing OWL component in Odoo 17 to extend its functionality, make targeted adjustments, and tailor it to meet your specific business requirements seamlessly.


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



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message