Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
3124 Weergaven

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
Annuleer
Auteur Beste antwoord

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
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
4
mrt. 24
3989
1
mrt. 24
1080
2
jul. 22
1792
1
jun. 22
2119
0
apr. 22
1435