Enable Dark Mode!
how-to-add-customer-rating-mixin-in-the-odoo-15.jpg
By: Aswathi C

How to Add Customer Rating Mixin in the Odoo 15

Technical Odoo 15

Customer rating is the feedback (issues, information, etc) given by your customers or users about their experience with your company, product, or services. This feedback can be later used by the company to improve its services or products. In odoo you can use customer rating using rating mixin. The customer rating mixin in odoo allows you to send emails to your customers asking for their ratings with feedback.

Adding rating on your model

For adding ratings on your module, you can simply inherit the rating.mixin model.
class TestModel(models.Models):
_name = 'your_model'
_inherit = ['rating.mixin', 'mail.thread']
user_id = fields.Many2one('res.users', 'Responsible')
partner_id = fields.Many2one('res.partner', 'Customer')
Now the features of mixin are available in your model.
  • After inheriting the rating.mixin model the rating.rating records are linked to the partner_id field on your model if the field exists. If you use another field name instead of partner_id (for res.partner model), the default behavior will be overridden by rating_get_partner_id() function.
  • If you inherit the mail.thread model on your model, then the chatter history will show the rating event.
    • Send rating requests using e-mail
  • If you want to send emails to request a rating from your customers, you can generate an email with links to rating objects. Below shows a simple template of a rating email template.
  • <record id="rating_my_model_email_template" model="mail.template">
    <field name="name">Test Model: Rating Request</field>
    <field name="email_from">${object.rating_get_rated_partner_id().email or '' | safe}</field>
    <field name="subject">Customer Rating Request</field>
    <field name="model_id" ref="test-module.your_model"/>
    <field name="partner_to" >${object.rating_get_partner_id().id}</field>
    <field name="auto_delete" eval="True"/>
    <field name="body_html"><![CDATA[
    % set access_token = object.rating_get_access_token()
    <p>Hi,</p>
    <p>Please take a moment to rate our services</p>
    <ul>
    <li><a href="/rate/${access_token}/5">Satisfied</a></li>
    <li><a href="/rate/${access_token}/3">Okay</a></li>
    <li><a href="/rate/${access_token}/1">Dissatisfied</a></li>
    </ul>
    ]]></field>
    </record>
  • Your customer will get an email that contains a link that redirects to a web page having a feedback form and rating. Then your customer can easily rate and send feedback to inform the users or company about their experience with the services or products.
You can then view the customer rating by defining an action window for rating as below. In the code below you can see that there are default views (kanban, pivot, graph) for ratings which allow you a quick bird’s eye view of your customer ratings.
<record id="rating_rating_action_my_model" model="ir.actions.act_window">
<field name="name">Customer Ratings</field>
<field name="res_model">rating.rating</field>
<field name="view_mode">kanban,pivot,graph</field>
<field name="domain">[('res_model', '=', test_module.your_model), ('res_id', '=', active_id), ('consumed', '=', True)]</field>
</record>
<record id="test_module_your_model_view_form_inherit_rating" model="ir.ui.view">
<field name="name">test_module.your_model.view.form.inherit.rating</field>
<field name="model">my_module.my_model</field>
<field name="inherit_id" ref="my_module.my_model_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<button name="%(rating_rating_action_my_model)d" type="action"
class="oe_stat_button" icon="fa-smile-o">
<field name="rating_count" string="Rating" widget="statinfo"/>
</button>
</xpath>
</field>
</record>
Example:
1. Sending customer rating requests as an email.
In this example, I am sending the email with the click of a button on the model. Here is the code for the function to send mail.
def send_rating_mail(self):
template = self.env.ref('db_backup.rating_test_model_email_template')
template.send_mail(self.env.user.id, force_send=True)
The template for email is as follows:
<record id="rating_test_model_email_template" model="mail.template">
<field name="name">Test Model: Rating Request</field>
<field name="model_id" ref="db_backup.model_db_backup"/>
<field name="subject">Customer Rating Request</field>
<field name="email_from">{{ (object.rating_get_rated_partner_id().email_formatted if
object.rating_get_rated_partner_id() else user.email_formatted) }}
</field>
<field name="partner_to">{{ object.rating_get_partner_id().id }}</field>
<field name="body_html" type="html">
<div>
<t t-set="access_token" t-value="object.rating_get_access_token()"/>
<t t-set="partner" t-value="object.rating_get_partner_id()"/>
<table border="0" cellpadding="0" cellspacing="0" width="590" style="width:100%; margin:0px auto;">
<tbody>
<tr>
<td valign="top" style="font-size: 13px;">
<t t-if="partner.name">
Hello <t t-out="partner.name or ''">Brandon Freeman</t>,
<br/>
<br/>
</t>
<t t-else="">
Hello,
<br/>
<br/>
</t>
Please take a moment to rate our services related to the task
<t t-if="object.rating_get_rated_partner_id().name">
assigned to <strong t-out="object.rating_get_rated_partner_id().name or ''">
Mitchell Admin</strong>.
<br/>
</t>
<t t-else="">.
<br/>
</t>
</td>
</tr>
<tr>
<td style="text-align: center;">
<table border="0" cellpadding="0" cellspacing="0" width="590"
summary="o_mail_notification" style="width:100%; margin: 32px 0px 32px 0px;">
<tr>
<td style="font-size: 13px;">
<strong>Tell us how you feel about our service</strong>
<br/>
<span style="text-color: #888888">(click on one of these smileys)</span>
</td>
</tr>
<tr>
<td style="font-size: 13px;">
<table style="width:100%;text-align:center;margin-top:2rem;">
<tr>
<td>
<a t-attf-href="/rate/{{ access_token }}/5">
<img alt="Satisfied"
src="/rating/static/src/img/rating_5.png"
title="Satisfied"/>
</a>
</td>
<td>
<a t-attf-href="/rate/{{ access_token }}/3">
<img alt="Okay"
src="/rating/static/src/img/rating_3.png"
title="Okay"/>
</a>
</td>
<td>
<a t-attf-href="/rate/{{ access_token }}/1">
<img alt="Dissatisfied"
src="/rating/static/src/img/rating_1.png"
title="Dissatisfied"/>
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top" style="font-size: 13px;">
We appreciate your feedback. It helps us to improve continuously.
<br/>
</td>
</tr>
</tbody>
</table>
</div>
</field>
<field name="auto_delete" eval="False"/>
</record>
The output of the template is shown below:
how-to-add-customer-rating-mixin-in-the-odoo-15-cybrosys

When clicking on the rating link it redirects to the website and shows a page like below:

how-to-add-customer-rating-mixin-in-the-odoo-15-cybrosys

2. Now define an action as below to see all the ratings.

<record id="rating_rating_action_test" model="ir.actions.act_window">

<field name="name">Customer Ratings</field>

<field name="res_model">rating.rating</field>

<field name="view_mode">kanban,pivot,graph</field>

<field name="domain">[('res_model', '=', 'db.backup'), ('res_id', '=', active_id)]</field>

</record>

The output of the above action will be as follows:

how-to-add-customer-rating-mixin-in-the-odoo-15-cybrosys

You can find default examples of customer ratings on the models project.task in the Project (rating_project) app and helpdesk.ticket in the Helpdesk (helpdesk - Odoo Enterprise only) app. Hope you all got the basic idea of using customer ratings on your custom modules. You can refer to the following blog link to know the mixins used in Odoo 15.



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

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