Skip to Content
Menu
This question has been flagged

Since Odoo 9 or 11, we use the feature "visible groups" for our website menus. Now in Odoo 16 the field is removed.


How to we restrict a menu item visibility to a certain user group in Odoo 16? Note, it's also about mege menu entries, page visibility *does not help*.

Avatar
Discard

Did you figure this out?

Author

@Matthew Harrison Yes, we did, see my answer below.

Author Best Answer

We developed a solution as part of https://apps.odoo.com/apps/modules/16.0/website_user_types/

The module makes the website menu tree visible outside debug mode. In the menus' form you can restrict a menu to specific website user groups, e.g. B2C and B2B. The important part is to consider the menu caching, too! In Odoo 16 the menu is cached and group visibility is not going to be recalculated without extending the t-cache. This can look like this:

< template
id="layout"
inherit_id="website.layout">
< xpath expr="//header/t[@t-cache]" position="attributes">
< attribute name="t-cache" add="website.get_website_user_group_cache_key()"/>
< /xpath>
< /template>

Depending on your use case you might add more groups. For us, the user groups fulfill the requirements.

Avatar
Discard

Hi Michael
What is the minimum things we need to change to show a menu based on group in website ?
I saw your module but it seems a lot of things to configure.
Can you provide a step by step guide to where to change ?
Thanks in Advance

Author

@Gouranga Kala

You need to add the groups field, add the security filter for portal and public users (see website_user_types/security/website_security.xml or implement another solution), and extend the cache key in the header according to the group combinations.

Best Answer

You can achieve it by inheriting the _compute_visible method of website.menu model.

Consider the below given example:-

class WebsiteMenu(models.Model):
_inherit = 'website.menu'


def _compute_visible(self):
"""Compute menu invisible"""
super()._compute_visible()
for menu in self:
if not menu.is_visible:
return
if menu.name == 'Shop' and not self.env.user.user_has_groups(
'base.group_user'):
menu.is_visible = False

Regards

Avatar
Discard
Author

Thanks four your post. Please note, that this solution will not work seamlessly in Odoo 16 because the menu is cached. Thus, every odoo worker might cache a different version of the menu and the menu shown to the user will be depending on the worker who answers the request, but not his user group. To extend t-cache, see my answer above.

Related Posts Replies Views Activity
1
May 25
1139
1
Apr 24
1660
0
Jul 24
256
1
Aug 23
1983
0
May 23
1520