Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
3851 Vistas

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
Descartar
Autor Mejor respuesta

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
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
4
mar 24
5194
1
mar 24
1747
2
jul 22
2412
1
jun 22
2952
0
abr 22
1801