How to show/hide a Button in Odoo 15 Pos Based on the User

How to show/hide a Button in Odoo 15 Pos Based on the User

2 minutes, 19 seconds Read

In Odoo POS, everything is defined in terms of screens. When coming to Odoo version 15, there are a lot of new features implemented like Limited Partner Loading, Customer, Ship Later, Refund, Open Product Info, etc. In this blog, we can check how to show or hide any button on the POS product screen based on the user’s access rights.

The POS ProductScreen is nothing but the screen where we can see the buttons like Discount, Info, Refund, Reward, etc. We can check how to hide any of these buttons for a specific user. For that, we can check how a button is defined on the product screen. For example, we can check how the RefundButton is defined in the ‘point_of_sale’ module’s RefundButton.js file. It is defined by extending the POS component and then it is added to the product screen as below:

ProductScreen.addControlButton({
   component: RefundButton,
   condition: function () {
       return true;
   },
});
From this code, we can understand that the RefundButton is added as a control button on the product screen. Then there is a condition function is added, which returns true. That means it depends on the condition defined, and it will show the button only when the condition returns true.
Here, no condition is defined, and true is directly returned without checking any condition. So it will return every time we open the screen.
Now we can check how to add our condition inside it. For that, we have to add a field in pos.config for adding the users who can see the Refund button like the below:
Inherit pos.config model and add the field
refund_employee_ids = fields.Many2many('hr.employee', string='Refund Employees')
It is a many2many field, so we can add more than one employee. Then we have to enable the login by employee feature in the point of sale configuration and fill in the Refund Employees field. Now the employees can log in to the system. The next is to set the condition for the refund button based on the current login user.
ProductScreen.addControlButton({
  component: RefundButton,
  condition: function () {
       var cashier = this.env.pos.get('cashier') || this.env.pos.get_cashier();
       has_refund = false;
       if (this.env.pos.config.refund_employee_ids.includes(cashier.id)) {
           has_refund = true;
       }
       return  has_refund;
  },
});
Now, we added a condition to check whether the cashier or the user has been included in the refund employees list or not. If the user is one among the users who have access to the refund button, the condition returns true. Thus the security condition for the Refund button is added. So the button will be hidden from normal users.
Like the above can set the security conditions on every button on the product screen, also known as the POS screen.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *