Enable Dark Mode!
how-to-override-an-existing-controller-in-odoo-15.jpg
By: Amal P

How to Override an Existing Controller in Odoo 15

Technical Odoo 15

In Odoo ERP, front-end modules are designed with the help of 'Controllers’. These frontend Controllers are connected to backend modules. We are using the property of overriding to make changes in the base class which is used to either make a change in the programming logic or to add new logic to the existing program. 

Through this blog, you will attain insight on how to override an existing controller in Odoo.

At first we create a folder named controller and all the files which deal with the controller are added to this folder. Inside a folder add an init file. All the python files inside the controller folder will be added in the init file and this will be mapped to the main init file. An example structure is given below.

how-to-override-an-existing-controller-in-odoo-15

In order to override a controller at first, we need to create a subclass and the existing function is defined in it and from that controller now we have to select a function for overriding.

For example, we can select a Carousel function for overriding.

def _get_products_recently_viewed(self):
"""
Returns list of recently viewed products according to current user
"""
max_number_of_product_for_carousel = 12
visitor = request.env['website.visitor']._get_visitor_from_request()
if visitor:
excluded_products = request.website.sale_get_order().mapped('order_line.product_id.id')
products = request.env['website.track'].sudo().read_group(
[('visitor_id', '=', visitor.id), ('product_id', '!=', False), ('product_id.website_published', '=', True), ('product_id', 'not in', excluded_products)],
['product_id', 'visit_datetime:max'], ['product_id'], limit=max_number_of_product_for_carousel, orderby='visit_datetime DESC')
products_ids = [product['product_id'][0] for product in products]
if products_ids:
viewed_products = request.env['product.product'].with_context(display_default_code=False).browse(products_ids)
FieldMonetary = request.env['ir.qweb.field.monetary']
monetary_options = {
'display_currency': request.website.get_current_pricelist().currency_id,
}
rating = request.website.viewref('website_sale.product_comment').active
res = {'products': []}
for product in viewed_products:
combination_info = product._get_combination_info_variant()
res_product = product.read(['id', 'name', 'website_url'])[0]
res_product.update(combination_info)
res_product['price'] = FieldMonetary.value_to_html(res_product['price'], monetary_options)
if rating:
res_product['rating'] = request.env["ir.ui.view"]._render_template('portal_rating.rating_widget_stars_static', values={
'rating_avg': product.rating_avg,
'rating_count': product.rating_count,
})
res['products'].append(res_product)
return res
return {}

At first, we need to import the existing class to our overriding file as a library

from Odoo.addons.website_sale.controllers.main import WebsiteSale
from Odoo.http import request

And now inside the subclass which is currently overriding we need to make the required changes like adding a function or altering a function by mentioning the function which we need to override and make the required changes to the function.

And in the case of our example we are changing the Carousel, the maximum number of products that can be viewed was 12 we are changing it to 10 by overriding it.

how-to-override-an-existing-controller-in-odoo-15

Now if you check the website you will be able to see only 10 products will be displayed in the colossal instead of 12.

how-to-override-an-existing-controller-in-odoo-15

Things we need to keep in mind while overriding an existing controller are:

- We're having mainly two ways to override an existing function either by replacing it or supering it. 

a) Function Overriding - we copy-paste the same function from the parent class to our inherited class and make the required changes in it, now the old function will be fully replaced with the new function.

b) Supering a function: this method is used when we need to inherit all the functionalities and properties of the function from the parent class and what we need is just to make some additions in the existing function.

- Syntax of inheriting a class is as given below:

from <path_to_existing_controller> import <ClassName>

For example:

from Odoo.addons.website_sale.controllers.main import WebsiteSale

- If what we need is just the values or to use or modify it, we can get it by rendering the template and we will get the values in the "qcontent" or **kwargs. Q context will return the value from the controller if we need, we can use them or we can modify it.

- If you are trying to override a controller that has a route we may face certain issues only if we did not specify the route in the inherited subclass. So we will have to mention the route in the inherited subclass and then you can make desired changes in it without any issue.

how-to-override-an-existing-controller-in-odoo-15

Here we override a function and change a product’s name with the help of Q context. You can override a function in Odoo 15 just as defined in the above example with ease.


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



0
Comments



Leave a comment

 
whatsapp
location

Calicut

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

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

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