Overriding Odoo18 Controllers
Odoo18 controllers handle web requests, interact with models and render the views for dynamic web applications. Overriding the existing controller in Odoo18 provides developers with the flexibility to customize page behavior and extend Odoo’s core functionality to suit specific business needs.
Learn how to override an existing controller in Odoo18 with Transines Solutions here
Create a Controller Directory
Start by confirming your custom module has a ‘controllers’ directory if it’s missing Create itand put place an ‘init.py’ file inside the ‘controllers’ directory.
Import the Existing Controller In Odoo18
To start customizing, open your new controller file (e.g., custom_controller.py) and import the relevant Odoo controller class that you wish to build up.
from odoo.addons.website_sale.controllers.main import WebsiteSale
from odoo.http import request
Create a Subclass and Override the Function
To change how an existing function works, create a new class that inherits all the properties of the original controller and then redefine the specific method you want to modify in this new class.
You can customize the description of the products by overriding _prepare_product_values, the method responsible for generating product details on the page.
class CustomWebsiteSale(WebsiteSale):
def _prepare_product_values(self, product, category='', search='', **kwargs):
values = super()._prepare_product_values(product, category, search, **kwargs)
values['custom_message'] = "Limited Time Offer!"
return values
Override a Function with a Route
If the original function you’re modifying is associated with a web address (route) the is essential to also declare the @http.route decorator in your overriding subclass.
Example: Modifying ‘/shop’ Page to Change a Product Name
from odoo import http
from odoo.addons.website_sale.controllers.main import WebsiteSale
class WebsiteShop(WebsiteSale):
@http.route(['/shop', '/shop/page/<int:page>'], type='http', auth="public", website=True)
def shop(self, page=0, category=None, search='', min_price=0.0, max_price=0.0, ppg=False, **post):
res = super(WebsiteShop, self).shop(page, category, search, min_price, max_price, ppg, **post)
# Modify product name using qcontext
products = res.qcontext.get('products', [])
for product in products:
if product.id == 9:
product.name = 'Normal Desk'
return res
Override Function Using context
- Use qcontext to customize template rendering by altering current parameters or adding new ones.
- Use qcontext to modify & access before rendering the values
- Modify the qcontext with the required values To include extra data in the response
Handle Route Definition
Overriding a controller function with a route
- For conflict avoidance, your inherited subclass must include the @http.route definition.
- In your subclass, clearly define the specific changes you want to make to extend or alter the existing functionality.
Difference Methods to Override a Odoo18 Function
Here are the two methods developers typically use to override a Odoo18 function
Function Overriding
This method shows to copying the existing function from the parent class into the inheriting class followed by the expected modifications.. particularly useful when a significant or complete change to the function’s logic is required.
Supering a Function
The super() method helps you to utlize the existing code from the parent class and apply only the small modifications, keeping the original function’s core logic.
This is all about to override existing controllers in Odoo18 through various techniques including using qcontext for minor changes, modifying routed functions, and complete overrides. Learning these methods enhances you to efficiently customize and extend Odoo’s core functionality to just meet your business requirements. For more details and technical assistance, please do not hesitate to contact us.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote