Odoo - how to add a new field in the customer portal and its values

Jeevachaithanyan Sivanandan - Nov 30 '23 - - Dev Community

we have a task that we need to display the linked purchase order for each sales order.

If the task says about customer portal, then it means the UI is in the website side, not Odoo backend. Here we need to add a new field in the customer portal - sales order, explains how do we do it.

  1. log in as a customer and then go to portal, in this case the url is
    http://localhost//my/orders

  2. so we need to take the '/my/orders' and search in the .py files as - @http.route(['/my/orders' because there will be line as this, since this is a web site request. We get some thing like this


@http.route(['/my/orders', '/my/orders/page/<int:page>'], type='http', auth="user", website=True)
def portal_my_orders(self, **kwargs):
values = self._prepare_sale_portal_rendering_values(quotation_page=False, **kwargs)
request.session['my_orders_history'] = values['orders'].ids[:100]
return request.render("sale.portal_my_orders", values)

  1. As it seems, it renders the template 'portal_my_orders' in the sale module, and we can see that its in the file sale_portal_templates with a line as below

<template id="portal_my_orders" name="My Sales Orders">

  1. we need to extend and inherit this template then add a new Table Head and Rows with values

  2. create a new template file in the current working module and inherit the template id as below

<template id="customer_portal_sales_order" inherit_id="sale.portal_my_orders" priority="50">
<xpath expr="//t/t[@t-if='orders']/thead/tr/th[2]" position="before">
<th class="text-left">Custom Field</th>
</xpath>
<xpath expr="//t/t[@t-foreach='orders']/tr/td[2]" position="before">
<td>
<span t-esc="order.get_custom_field()"/>
</td>
</xpath>
</template>

so we used xpath to find the location that we need to insert the new field then used the t-esc directive to call a custom method that will return the values we need to put in the rows.

  1. lets create the custom method, this method should be in the sale.order model file and method is as below
    def get_custom_field(self):
        domain = [ ('origin' , '=' , self.name) ]
        purchase_id = self.env [ 'purchase.order' ].search ( domain , limit = 1 )
        if purchase_id :
            return purchase_id.partner_ref
        elif not purchase_id.partner_ref :
            return 'No values entered'
        else :
            return 'No Such Field'
Enter fullscreen mode Exit fullscreen mode

domain is the condition that we have to search and we are picking the purchase orders with that condition and return the partner reference field value

  1. Save everything, upgrade the module and restart Odoo server, it should work
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player