Enable Dark Mode!
how-to-customize-product-filters-of-dynamic-snippets-in-e-commerce-in-odoo-16.jpg
By: Salman H

How to Customize Product Filters of Dynamic Snippets in E-commerce in Odoo 16

Technical Odoo 16

Dynamic snippets are an integral part of Odoo's website builder. They enable users to create dynamic content blocks that can adapt to different situations and user interactions. These dynamic snippets can be used to display products, services, events, or any other content that may change over time. However, to make these snippets truly effective, it's essential to provide users with the ability to filter and sort the displayed content according to their preferences.

Customization of Dynamic Snippet Filters

Customizing dynamic snippet filters in Odoo 16 empowers businesses to tailor their website content more effectively. Let's delve into the steps required to achieve this customization.

First, we have to create a server action that calls the function which fetches the data for the filter. Then we have to create a filter in the backend as a record of the ‘website.snippet.filter’ model and add the server action in the field ‘action_server_id’.

<?xml version="1.0" encoding="utf-8"?>
<odoo>
   <data>
       <record id="dynamic_snippet_customize_filter_action"  model="ir.actions.server">
           <field name="name">Customize Filter</field>
           <field name="model_id" ref="product.model_product_product"/>
           <field name="state">code</field>
           <field name="code">
DynamicFilter = model.env['website.snippet.filter']
response = DynamicFilter._get_products('customize_filter', model.env.context)
           </field>
       </record>
<record id="dynamic_filter_customize_filter"      model="website.snippet.filter">
           <field name="action_server_id" ref="your_module.dynamic_snippet_customize_filter_action"/>
         <field name="field_names">
display_name,description_sale,image_512,price:monetary</field>
           <field name="limit" eval="16"/>
           <field name="name">Customize Filter Products</field>
       </record>
   </data>
</odoo>

Please remember to add the xml file to manifest under data files. The server action calls the method ‘_get_products()’ defined inside the model ‘website.snippet.filter’.

@api.model
def _get_products(self, mode, context):
   dynamic_filter = context.get('dynamic_filter')
   handler = getattr(self, '_get_products_%s' % mode, self._get_products_latest_sold)
   website = self.env['website'].get_current_website()
   search_domain = context.get('search_domain')
   limit = context.get('limit')
   domain = expression.AND([
       [('website_published', '=', True)],
       website.website_domain(),
       [('company_id', 'in', [False, website.company_id.id])],
       search_domain or [],
   ])
   products = handler(website, limit, domain, context)
   return dynamic_filter._filter_records_to_values(products, False)

This method is defined inside the ‘website.snippet.filter’ model. Here the value of parameter ‘mode’ is ‘customize_filter’. In the statement handler = getattr(self,

'_get_products_%s' % mode, self._get_products_latest_sold) 

We call the function ‘_get_products_customize_filter()’ which we will define in our custom module inside the ‘website.snippet.filter’ model.

def _get_products_customize_filter(self, website, limit, domain, context):
   products = []
   visitor = self.env['website.visitor']._get_visitor_from_request()
   if visitor:
       excluded_products = website.sale_get_order().order_line.product_id.ids
       tracked_products = self.env['website.track'].sudo().read_group(
           [('visitor_id', '=', visitor.id), ('product_id', '!=', False), ('product_id.website_published', '=', True), ('product_id', 'not in', excluded_products)],
           ['product_id', 'visit_datetime:max'], ['product_id'], limit=limit, orderby='visit_datetime DESC')
       products_ids = [product['product_id'][0] for product in tracked_products]
       if products_ids:
           domain = expression.AND([
               domain,
               [('id', 'in', products_ids)],
           ])
           products = self.env['product.product'].with_context(display_default_code=False, add2cart_rerender=True).search(domain, limit=limit)
   return products

In this function, we can define which products should be shown in the dynamic snippet. Here the function filters out the recently viewed products. 

After this let's open a dynamic snippet and see if our filter is visible inside the options.

how-to-customize-product-filters-of-dynamic-snippets-in-e-commerce-in-odoo-16-1-cybrosys

Here we can see the products in the dynamic snippet which are shown according to the default filter ‘Newest Products’. how-to-customize-product-filters-of-dynamic-snippets-in-e-commerce-in-odoo-16-2-cybrosys

And when we open the snippet option there under filters we can see our custom filter.

Now let's select the custom filter and see what happens.

how-to-customize-product-filters-of-dynamic-snippets-in-e-commerce-in-odoo-16-3-cybrosys

And when the filter is applied the snippet shows the recently viewed products as we can see.

Customization of dynamic snippet filters in Odoo 16 opens up new possibilities for creating tailored and user-friendly website content. By following the steps outlined in this blog post and using the provided technical examples, businesses can offer their website visitors a more personalized experience. Whether you're managing an e-commerce site, a blog, or any other type of web platform, Odoo 16's dynamic snippet filter customization empowers you to engage your audience effectively.

To read more about creating a dynamic carousel using the chunk function in Odoo 16, refer to our blog How to Create Dynamic Carousel Using Chunk Function in Odoo 16


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message