omas
omas

Reputation: 79

How to create a WEBSITE SEARCH in [odoo12]

I want to create a search field in a website in odoo that will allow me to search for the names of customers but I don't know how to add a function to do that. Can you please guide me to a tutorial that can help me in the creation of my search function.

I checked in the module website_sale, I tried to do the same but it did not really work.

Here is the code I put:

class Controllers(http.Controller):

    @api.multi
    def sale_product_domain(self):
        return self.get_current_website().website_domain()

    def _get_search_domain(self, search, attrib_values):
        domain = Controllers.sale_product_domain(self)
        if search:
            for srch in search.split(" "):
                domain += [
                    '|', '|', '|', ('name', 'ilike', srch), ('description', 'ilike', srch)]

        if attrib_values:
            attrib = None
            ids = []
            for value in attrib_values:
                if not attrib:
                    attrib = value[0]
                    ids.append(value[1])
                elif value[0] == attrib:
                    ids.append(value[1])
                else:
                    domain += [('attribute_line_ids.value_ids', 'in', ids)]
                    attrib = value[0]
                    ids = [value[1]]
            if attrib:
                domain += [('attribute_line_ids.value_ids', 'in', ids)]

        return domain
@http.route('/tabs', type='http', auth='public', website=True)
    def navigate_to_tabs(self, search='', ppg=False, **post):
        clients = http.request.env['res.partner'].sudo().search([])
        if ppg:
            try:
                ppg = int(ppg)
            except ValueError:
                ppg = PPG
            post["ppg"] = ppg
        else:
            ppg = PPG

        attrib_list = request.httprequest.args.getlist('attrib')
        attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v]
        attributes_ids = {v[0] for v in attrib_values}
        attrib_set = {v[1] for v in attrib_values}

        domain = self._get_search_domain(search, attrib_values)

        keep = QueryURL('/tabs', search=search, attrib=attrib_list,
                        )

        url = "/tabs"
        if search:
            post["search"] = search
        if attrib_list:
            post['attrib'] = attrib_list

        Partner = request.env['res.partner']

        Category = request.env['product.public.category']
        search_categories = False
        if search:
            categories = Partner.search(domain).mapped('public_categ_ids')
            search_categories = Category.search(
                [('id', 'parent_of', categories.ids)] + request.website.website_domain())
            categs = search_categories.filtered(lambda c: not c.parent_id)
        else:
            categs = Category.search([('parent_id', '=', False)] + request.website.website_domain())

        parent_category_ids = []

        values = {
            'search': search,
            #'category': category,
            'attrib_values': attrib_values,
            'attrib_set': attrib_set,
            # 'search_count': product_count,  # common for all searchbox
            'rows': PPR,
            'categories': categs,
            'keep': keep,
            'parent_category_ids': parent_category_ids,
            'search_categories_ids': search_categories and search_categories.ids,
        }

        return http.request.render('site_web.s_tabs', {'clients' : clients})

Upvotes: 0

Views: 363

Answers (1)

Avani Somaiya
Avani Somaiya

Reputation: 348

I'd recommend looking into this project for website_search reference purpose:

https://github.com/OpenSur/Odoo_addons/tree/master/website_search

As it aims to provide an all-in-one search interface for pages, blogs posts, blog post comments, customers, job opportunities and products. I think it provides a solid base for customizing based on your specific requirements. You can take reference from this module.

I hope this helps you. Thank you.

Upvotes: 1

Related Questions