-1

I found a module that autocomplete search in a Website e-commerce with high-light match words and image. But I did not really understand what each command do .

Can you please explain to me how this code work, and why they did /shop/get_suggest?

class WebsiteSale(http.Controller):
    @http.route(['/shop/get_suggest'], type='http', auth="public", methods=['GET'], website=True)
    def get_suggest_json(self, **kw):
        query = kw.get('query')
        names = query.split(' ')
        domain = ['|' for k in range(len(names) - 1)] + [('name', 'ilike', name) for name in names]
        products = request.env['product.template'].search(domain, limit=15)
        products = sorted(products, key=lambda x: SequenceMatcher(None, query.lower(), x.name.lower()).ratio(),
                          reverse=True)
        results = []
        for product in products:
            results.append({'value': product.name, 'data': {'id': product.id, 'after_selected': product.name}})
        return json.dumps({
            'query': 'Unit',
            'suggestions': results
        })
omas
  • 69
  • 1
  • 12

1 Answers1

0

This controller function will be activated when you load page your_domain/shop/get_suggest.

The function just searches for products with similar name of the query given in the search.

Please go through this documentation to learn basics of building a website

Amal
  • 244
  • 3
  • 9
  • Thank you a lot for your response. It helps me a lot. I want to create a method that show customer informations like in shop(it shows product informations). Can you please guide me to the method that do that in website_sale module so I can be inspired – omas Jun 19 '19 at 12:04