Enable Dark Mode!
how-to-add-search-in-customer-portal-in-odoo-18.jpg
By: Amaya Aravind EV

How to Add Search in Customer Portal in Odoo 18

Functional Odoo 18

You can add search options for particular items, articles, or support documents with Odoo's flexible solution for improving user experience on your internet portal. Without a search function, they could have to read through several pages or manually go through categories, which could be frustrating and cost them business. Including a search option improves user experience and boosts portal engagement by enabling users to find what they're looking for quickly.

When you click "My Account" in Odoo, the customer portal appears. Here, you can access services like purchase orders, sale orders, quotes, and more. You can view additional information by selecting each menu.

We've added our custom menu to the "Recruitment" customer portal.

How to Add Search in Customer Portal in Odoo 18-cybrosys

Use the following XML template to add a custom menu to the customer portal website: Create a directory for view and add this code an xml file and add its path in the module manifest.

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <template id="portal_recruitment" name="Recruitment"
              inherit_id="portal.portal_breadcrumbs" priority="30">
        <xpath expr="//ol[hasclass('o_portal_submenu')]" position="inside">
            <li t-if="page_name == 'recruitment'"
                t-attf-class="breadcrumb-item #{'active ' if recruitment else ''}">
                <a t-if="recruitment"
                   t-attf-href="/recruitment?{{ keep_query() }}">Recruitment
                </a>
                <t t-else="">Recruitment</t>
            </li>
        </xpath>
    </template>
    <template id="portal_my_home_menu_recruitment" name="Recruitment"
              inherit_id="portal.portal_my_home"
              customize_show="True" priority="30">
        <xpath expr="//div[hasclass('o_portal_docs')]" position="before">
            <t t-set="portal_client_category_enable" t-value="True"/>
        </xpath>
        <div id="portal_client_category" position="inside">
            <t t-call="portal.portal_docs_entry">
                <t t-set="icon"
                   t-value="'/portal_recruitment/static/src/img/recruitment.svg'"/>
                <t t-set="title">Recruitment</t>
                <t t-set="url" t-value="'/Recruitment'"/>
                <t t-set="text">View the Recruitments</t>
                <t t-set="placeholder_count" t-value="'portal_recruitment'"/>
            </t>
        </div>
    </template>
    <template id="portal_my_home_recruitment_views" name="My Time Off">
        <t t-call="portal.portal_layout">
            <t t-set="breadcrumbs_searchbar" t-value="True"/>
            <t t-call="portal.portal_searchbar">
                <t t-set="title">Recruitment</t>
            </t>
            <t t-call="portal.portal_table">
                <thead>
                    <tr class="active">
                        <th class="text-left">Applicant</th>
                        <th class="text-center">Create Date</th>
                        <th class="text-center">Job Position</th>
                        <th class="text-end">Status</th>
                    </tr>
                </thead>
                <t t-foreach="recruitment" t-as="record">
                    <tr>
                        <td class='text-left'>
                            <span t-field="record.partner_name"/>
                        </td>
                        <td class='text-center'>
                            <span t-field="record.create_date"/>
                        </td>
                        <td class='text-center'>
                            <span t-field="record.job_id.name"/>
                        </td>
                        <td class='text-end'>
                            <span t-field="record.stage_id.name"/>
                        </td>
                    </tr>
                </t>
            </t>
        </t>
    </template>
</odoo>

We need to define a function in the Python controller in order to generate and show the custom menu. The following code can be used to do this. We can create a Python file named main.py inside the controller directory for this.

from odoo import http
from odoo.http import request
from odoo.addons.portal.controllers import portal

class WebsiteEvents(portal.CustomerPortal):
    """To get the recruitments in the website portal"""
    def _prepare_home_portal_values(self, counters):
        """Supering the method to add function for search."""
        values = super()._prepare_home_portal_values(counters)
        if 'portal_recruitment' in counters:
            values['portal_recruitment'] = request.env[
                'hr.applicant'].sudo().search_count(
                [('user_id', '=', request.env.uid)])
        return values

We must write a controller function for the specific URL that we provided in the template.

@http.route(['/Recruitment', '/Recruitment/page/<int:page>'], type='http',auth="user", website=True)
def portal_recruitment(self, search=None, search_in='All'):
    """To search the recruitments data in the portal"""
    searchbar_inputs = {
    'All': {'label': 'All', 
            'input': 'All', 
            'domain': []},
            'Job Position': {'label': 'Job Position', 
                             'input': 'Job Position',
                             'domain': [('job_id', 'like', search)]},
            'Status': {'label': 'Status', 'input': 'Status',
                       'domain': [('stage_id', 'like', search)]},
    }
    search_domain = searchbar_inputs[search_in]['domain']
    recruitment = request.env['hr.applicant'].sudo().search([
    ('user_id', '=', request.env.uid)])
    search_recruitments = recruitment.search(search_domain)
    return request.render(
   'Website_search_bar_in_customer_portal.portal_my_home_recruitment_views',
              {'recruitment': search_recruitments,
              'page_name': 'recruitment',
              'search': search,
               'search_in': search_in,
               'searchbar_inputs': searchbar_inputs
               })

The request.render is used to render the template that we added. So, the path should be in module_name.template_name format.

Label: Specifies the search option's display label.

Input: The value entered concerning this search option is defined by the input.

Domain: Specifies the domain filter to be applied during the search.

The search option with searching values is visible on the right side, and clicking on the menu will bring up the view with the records that follow.

How to Add Search in Customer Portal in Odoo 18-cybrosys

The records are then extracted based on the value of the search that we want to use.

search_domain = searchbar_inputs[search_in]['domain']
        recruitment = request.env['hr.applicant'].sudo().search([
            ('user_id', '=', request.env.uid)])
        search_recruitments = recruitment.search(search_domain)
        return request.render('website_search_bar_in_customer_portal.portal_my_home_recruitment_views',
                {
                    'recruitment': search_recruitments,
                    'page_name': 'recruitment',
                    'search': search,
                    'search_in': search_in,
                    'searchbar_inputs': searchbar_inputs
                })

We must supply this order value in the search() method for the searching procedure to run correctly. Returning the values to the XML includes the search, search_in, and searchbar_inputs.

By selecting the "Search" option, we can now look through the entries based on the indicated need.

How to Add Search in Customer Portal in Odoo 18-cybrosys

In this case, the records are retrieved based on the job position during the hiring process. You can add the search feature to your Odoo internet gateway and modify the search functionality to suit your needs by following the steps described in this article.

To read more about How to Add Search in Customer Portal in Odoo 17, refer to our blog How to Add Search in Customer Portal in Odoo 17.


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