Skip to Content
Menu
This question has been flagged
1 Reply
10337 Views

Hello everyone, 

i'm building a snippet that display latest published products (ecommerce products)

ok, let's start 

addons/my_theme/views/snippets.xml

<template id="ecommerce_list" name="Ecommerce List" inherit_id="website.snippets">
<xpath expr="//div[@id='snippet_structure']/div[@class='o_panel_body']" position="inside">
<t t-snippet="my_theme.latest_products_list"/>
</xpath>
</template>

addons/my_theme/views/templates.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>

<template id="latest_products_list" name="Ecommerce List">
<section>
<div classe="row">

<h2> Latest Produtcts </h2>
<t t-foreach="values" t-as="product">
<h2 t-field="product.name"/>
<img t-attf-src="data:image/*;base64,{{product.image_medium}}"/>
</t>

</div>
</section>
</template>

<template id="snippet_js" inherit_id="website.assets_editor" name="Snippet js">
<xpath expr="." position="inside">
<script type="text/javascript" src="/my_theme/static/src/js/snippet.js" />
</xpath>
</template>

</odoo>

addons/my_theme/controllers/controllers.py

# -*- coding: utf-8 -*-
from odoo import http
from odoo.addons.website_sale import models, views
from odoo.http import request
class MyTheme(http.Controller):
@http.route(['/my_theme/ecommerce_list'], type="json", auth="public", website="True")
def list(self, **kw):
result = request.env['product.product'].search([])
return request.render('my_theme.latest_products_list',{'values' : result })

i tried my best to understand how should i can continue here,
after a few search i concluded that i must send json request  from javascript to this url route "/my_theme/ecommerce_list", and the controller will render my snippet template "my_theme.latest_products_list" that is declared in "addons/my_theme/views/templates.xml"

"i'm not sure is this the right way or there are other solutions that are easier than this" 

this is my main confusion !!!   really need your help to i understand more about odoo logic

​so, i tried many JS codes and i'm done here

addons/my_theme\static\src\js\snippet.js​

​​odoo.define('my_theme.snippet', function (require) {
'use strict';

var core = require('web.core');
var sAnimation = require('website.content.snippets.animation');

var _t = core._t;
var ajax = require(web.ajax);

sAnimation.registry.js_send_json_request = sAnimation.Class.extend({
function (require){
ajax.jsonRpc("/my_theme/ecommerce_list", 'call')
}
});
});
Avatar
Discard

in my case 

self.view.$el.append(html_from_server)

but does not working.    TypeError: Cannot read property '$el' of undefined 

Best Answer

I am impressed by your efforts to make it work and then ask for help at the end.

I am not a good web developer, though I will try to help you at my best.


Controller:

First you need to pass the some kind of JSON data from the controller. You want to display the product details. So better to prepare a list of dictionary having product details you want to display. 

Like: [{'name': 'abc', 'code': 'test', 'price': 123, ....}]


JS:

From the JS, you need to call / render you web template after calling the controller and pass the json data to the template as follow:

Ex:

ajax.jsonRpc('web/get_cart_details', 'call', {}).done(function() {
}).then(function (json_data) {
self.view.$el.append(QWeb.render('view_cart_detail_template', {'product_details': json_data}))              
    // $("#ViewCartModal").modal(); // Display modal
return ;
})


Qweb Template:

In the code, you need to pass the product details to your web template and use product_details variable in the template in the loop as it is a list of dict.

Ex:

<tr t-foreach="product_details" t-as="prod_data">
    <td> <t t-esc="prod_data.name"/> </td> // Use the keys of the dictionary
    <td> <t t-esc="prod_data.code"/> </td>
</tr>

This way you can pass the data from Controller to JS and from JS to Qweb Template.

Hope this will help you at some level.

Avatar
Discard

can this be used with a module instead of a controller