İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
3895 Görünümler

In the e-commerce address form the country found by geoip is set as selected. Which is convenient for the user.


Using the form builder for website on the other hand, when creating a form with a country field. The first option (Afghanistan) is selected as default. Yes i can 'hardcode' another country as a default by selecting it in the wysiwyg editor. But i would like to base it on geoip.


Any idea where i have to start looking for this ?


The default for many2one seems to be set here:

https://github.com/odoo/odoo/blob/cb9314e902fc899c5ab42b6fd4d8c1ed811c24e7/addons/website/static/src/xml/website_form_editor.xml#L307


But where exactly can i set the record.selected by geopi when the select name is 'country_id' ?


Thanks            

Avatar
Vazgeç
Üretici En İyi Yanıt

As the web-editor generates a 'static' html snippet stored in the database, it is not evaluated when loaded. So this 'quick trick' uses client javascript.

Made the country name available : (whichi is used as the option.text() )

class Http(models.AbstractModel):
_inherit = 'ir.http'


@api.model
def get_frontend_session_info(self):
session_info = super(Http, self).get_frontend_session_info()
session_info.update({
'geoip_country_name': request.session.get('geoip', {}).get('country_name'),
})
return session_info 


And using that i set the country in the form to the country found by geoip:


/** @odoo-module */

import { session } from '@web/session';

$(document ).ready(function() {
const slct = $('select[name=country_id].s_website_form_input');
if(slct.length){
if (session.geoip_country_name){
$('select[name=country_id].s_website_form_input option').filter(function(){
return $(this).text() == session.geoip_country_name;
}).prop('selected',true);
}
}
});


Enhancements: pass on the country database id for the country record, as those numeric values are used as the option.val()


Anyway, perhaps it is helpfull for someone else.



Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
4
Mar 24
5256
1
Mar 24
1792
2
Tem 22
2467
1
Haz 22
2991
0
Nis 22
1843