From 3c75c057d161ca23710ef5970c8d6ec91fac3f76 Mon Sep 17 00:00:00 2001 From: Siddharth Bhalgami Date: Fri, 23 Mar 2018 15:37:12 +0530 Subject: [PATCH] [ADD] Manage product category visibility on website by partners/customers tags! If specific tag(s) of partner/customer matches with product category's ``Partner Tags`` field then, that product category will be visible to that partner/customer in website/E-commerce. Partner Tags Hierarchy will also work... For Ex. -> ``Partner`` is Parent Tag -> ``Partner/Gold`` is Child Tag If Any Partner/Customer which have Tag of ``Partner``, then automatically it'll also shows the prodcut category with ``Partner/Gold``, because ``Partner/Gold`` is Child Of ``Partner`` tag. Also, If Main Product Category have Partner Tag then that tag will automatically apply to its Child Category & their products. For More Info: Issue - https://gitlab.com/flectra-hq/flectra/issues/45 --- addons/website_sale/controllers/main.py | 40 +++++++++++++++++-- addons/website_sale/models/product.py | 11 +++++ .../website_sale/security/ir.model.access.csv | 1 + addons/website_sale/views/product_views.xml | 1 + addons/website_sale/views/templates.xml | 8 +++- 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index ccd9223e..24ffbf13 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -239,7 +239,6 @@ class WebsiteSale(http.Controller): compute_currency, pricelist_context, pricelist = self._get_compute_currency_and_context() request.context = dict(request.context, pricelist=pricelist.id, partner=request.env.user.partner_id) - url = "/shop" if search: post["search"] = search @@ -252,7 +251,34 @@ class WebsiteSale(http.Controller): if attrib_list: post['attrib'] = attrib_list - categs = request.env['product.public.category'].search([('parent_id', '=', False), ('website_ids', 'in', request.website.id)]) + current_partner_tags = request.context['partner'].category_id + partner_child_tags = request.env['res.partner.category'].search( + [('parent_id', 'in', current_partner_tags.ids)]) + + if not request.env.user.has_group('website.group_website_publisher'): + categs = request.env['product.public.category'].search( + [('parent_id', '=', False), + ('website_ids', 'in', request.website.id), + '|', ('partner_tag_ids', 'in', + current_partner_tags.ids + partner_child_tags.ids), + ('partner_tag_ids', '=', False)]) + else: + categs = request.env['product.public.category'].search( + [('parent_id', '=', False), + ('website_ids', 'in', request.website.id)]) + + categs_with_childs = request.env['product.public.category'].search( + [('website_ids', 'in', request.website.id), + '|', ('partner_tag_ids', 'in', + current_partner_tags.ids + partner_child_tags.ids), + ('partner_tag_ids', '=', False)]) + + parent_categ_with_childs = request.env['product.public.category'].\ + search([('parent_id', 'in', categs_with_childs.ids), + '|', ('partner_tag_ids', 'in', + current_partner_tags.ids + partner_child_tags.ids), + ('partner_tag_ids', '=', False)]) + Product = request.env['product.template'] parent_category_ids = [] @@ -263,10 +289,16 @@ class WebsiteSale(http.Controller): parent_category_ids.append(current_category.parent_id.id) current_category = current_category.parent_id + if not request.env.user.has_group('website.group_website_publisher') \ + and (categs_with_childs or parent_categ_with_childs): + domain += ['|', '&', + ('public_categ_ids', 'in', categs_with_childs.ids), + ('public_categ_ids', 'in', parent_categ_with_childs.ids), + ('public_categ_ids', '=', False)] + product_count = Product.search_count(domain) pager = request.website.pager(url=url, total=product_count, page=page, step=ppg, scope=7, url_args=post) products = Product.search(domain, limit=ppg, offset=pager['offset'], order=self._get_search_order(post)) - ProductAttribute = request.env['product.attribute'] ProductBrand = request.env['product.brand'] ProductTag = request.env['product.tags'] @@ -302,6 +334,8 @@ class WebsiteSale(http.Controller): 'bins': TableCompute().process(products, ppg), 'rows': PPR, 'categories': categs, + 'categories_with_child': categs_with_childs.ids + + parent_categ_with_childs.ids, 'attributes': attributes, 'compute_currency': compute_currency, 'keep': keep, diff --git a/addons/website_sale/models/product.py b/addons/website_sale/models/product.py index 83a5b296..7f98d048 100644 --- a/addons/website_sale/models/product.py +++ b/addons/website_sale/models/product.py @@ -131,6 +131,17 @@ class ProductPublicCategory(models.Model): string='Websites', copy=False, help='List of websites in which ' 'category will published.') + partner_tag_ids = fields.Many2many('res.partner.category', + 'partner_public_categ_tags_rel', + 'tag_id', 'category_id', + string='Partner Tags', + help='If logged in customers/partners ' + 'have this tag then this product ' + 'category will appear to them in ' + 'E-commerce website.\n\n' + 'If empty then it becomes general ' + 'category which display to any ' + 'customers/partners.') @api.model def create(self, vals): diff --git a/addons/website_sale/security/ir.model.access.csv b/addons/website_sale/security/ir.model.access.csv index 1a780c75..e425b8ba 100644 --- a/addons/website_sale/security/ir.model.access.csv +++ b/addons/website_sale/security/ir.model.access.csv @@ -22,3 +22,4 @@ access_product_view_limit,access_product_view_limit,model_product_view_limit,,1, access_product_ribbon,product_ribbon,model_product_ribbon,,1,0,0,0 access_product_tags,access_product_tags,model_product_tags,,1,0,0,0 access_product_brand,access_product_brand,model_product_brand,,1,0,0,0 +access_res_partner_category_group_public,res_partner_category_group_public,base.model_res_partner_category,,1,0,0,0 diff --git a/addons/website_sale/views/product_views.xml b/addons/website_sale/views/product_views.xml index 15f9a999..6bb59e4e 100644 --- a/addons/website_sale/views/product_views.xml +++ b/addons/website_sale/views/product_views.xml @@ -156,6 +156,7 @@ + diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml index 2062fdc1..cfc4e59c 100644 --- a/addons/website_sale/views/templates.xml +++ b/addons/website_sale/views/templates.xml @@ -362,7 +362,13 @@