Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
3121 Vizualizări

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            

Imagine profil
Abandonează
Autor Cel mai bun răspuns

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.



Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
4
mar. 24
3984
1
mar. 24
1080
2
iul. 22
1791
1
iun. 22
2117
0
apr. 22
1435