Dynamic snippets are an important part of Odoo's website design. It allows customers to create dynamic content blocks that may adapt to extraordinary conditions and person interactions. these dynamic snippets may be used to display products, services, activities, or other content material that can trade over time. But for these snippets to be truly effective, users need to be given the ability to filter and sort the content they see based on their preferences.
Customization of Dynamic Snippet Filters
Customizing dynamic snippet filters in Odoo 17 allows businesses to customize their website content better. Let's examine the steps required to make this change.
First, we have to create a server action that calls the function which fetches the data for the filter. Then, we need to create a filter according to the file in the "website.snippet.filter" structure in the backend and add the server action to the "action_server_id" field.
<?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="dynamic_snippet_customize_filter_action"/>
<field name="field_names">display_name,description_sale,image_512</field>
<field name="limit" eval="16"/>
<field name="name">Customize Filter Products</field>
</record>
</data>
</odoo>
Be sure 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 = [
('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 "_get_products_customize_filter()" function in 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'], limit=limit, order='visit_datetime:max DESC')
products_ids = [product.id 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 feature filters newly found items.
Then, we open the dynamic snippet and see if our filter is visible inside the options.
Here, we can see the products shown in the dynamic clip according to the default "Latest Products" filter.
And when we open the snippet option under filters, we can see our own filter.
Now, let's select the custom filter and see what happens.
After applying the filter, the column shows newly found items.
A special dynamic particle filter in Odoo 17 opens up new possibilities for creating custom and user-friendly web content. Follow the steps outlined in this report and use technology-enabled examples to show how businesses can deliver a better experience to their visitors. Whether you manage an e-commerce site, a blog, or any other type of web platform, customizing Odoo 17's dynamic snippet filter allows you to effectively engage your audience.