Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
5770 Widoki

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*.

Awatar
Odrzuć

Did you figure this out?

Autor

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

Autor Najlepsza odpowiedź

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.

Awatar
Odrzuć

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

Autor

@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.

Najlepsza odpowiedź

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

Awatar
Odrzuć
Autor

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.

Powiązane posty Odpowiedzi Widoki Czynność
1
maj 25
10236
1
maj 25
1880
1
kwi 24
2507
0
lip 24
256
1
sie 23
2984