Hey Cyllo community! How can I limit the amount of data displayed in a dynamic website snippet? I'm getting too much info and need to trim it down. Any tips?
Welcome!
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.
This question has been flagged
Here's a solution for creating a dynamic product snippet in Odoo. It uses a JSON route, XML templates, and an OWL widget to fetch and display product data.
from odoo import http
from odoo.http
import request
class WebsiteProduct(http.Controller):
@http.route('/get_products', auth="public", type='json', website=True)
def get_products(self):
products = request.env['product.template'].sudo().search_read(
[('list_price', '<=', 10)], fields=['name', 'image_1920', 'id'], limit=5
)
values = {
'products': products
}
return values
This controller defines the '/get_products' route, fetching product data and returning it as JSON.
These XML templates define a placeholder and register the snippet.
Your Product snippet will be displayed here...
Please save to view the snippet
Products
product.id">
t-att-alt="product.name or 'Product Image'"
loading="lazy"/>
alt="Default Product Image"
loading="lazy"/>
/** @odoo-module */
import { renderToElement } from "@web/core/utils/render";
import publicWidget from "@web/legacy/js/public/public_widget";
import { rpc } from "@web/core/network/rpc";
publicWidget.registry.Products = publicWidget.Widget.extend({
selector: ".products_section",
async willStart() {
const result = await rpc('/get_products', {});
if(result){
this.$target.empty().html(
renderToElement('dynamic_snippet.products_data', { result: result })
);
}
},
}); The above QWeb template renders the product data.
Finally, this OWL widget fetches data via RPC and updates the DOM.