Skip to Content
Menu
This question has been flagged
3 Replies
5211 Views

Hello everyone.

Hope you're all fine and safe.

I am using Odoo 12 and have the need to make a change on the website e-commerce module (website_sale) for the customer address when on the checkout process.

How can I set a default country for the "Country" field?

Thank you all in advance

Best regards and keep safe

PM



Avatar
Discard
Best Answer

Before Odoo 12, your best option was to install the OCA module "eCommerce default country" : https://apps.odoo.com/apps/modules/11.0/website_sale_default_country/

From Odoo 12 you don't need that module, because the default country is allways related to public_user or connected user. In the case of public_user, the default country is requested from geoip. See this code:

https://github.com/odoo/odoo/blob/2a99a28004f8fcec96c73f847cbb6f3939fec4ab/addons/website_sale/controllers/main.py#L638-L646

Avatar
Discard
Author

Thank you Xavier Brochard,

I have noticed that later because I was testing offline and always have to change country. I later noticed on the code you provided the geoip implementation.

Thank you very much

Regards

PM

Best Answer

Hi Paulo, To be honest I am not an odoo developer.

As far as I know, the quick way(but not good way) is to edit the template in web UI.

step 1: go to "HTML/CSS editor ", see picture below.



Step 2: find the ID of country you want. For example, Fuji is ID 71.

Step 3:  change the line as in the picture



HOpe help you.

Avatar
Discard
Author

Great Jiashun Chen,

This is a good approach and solved my issue for now.

Thank you very much.

Anyway, I have found that can be done in python by inheriting the Websale class for the website_sale module.

I also discovered that there is an implementation that automatically detects the country for the visitor. Since I was testing offline, I did not noticed. But, anyway I am using your approach but in python we can:

Under "controllers/main.py" on website_sale module:

country_code = request.session['geoip'].get('country_code')

if country_code:

def_country_id = request.env['res.country'].search([('code', '=', country_code)], limit=1)

else:

def_country_id = request.website.user_id.sudo().country_id

We can change the full logic above with something like:

def_country_id = request.website.company_id.sudo().country_id.code

This way, the default country will be the company country.

Problem is, I do not know how do it by inheriting this piece of code on a new module. What I have done so far, and stucked:

from odoo.addons.website_sale.controllers.main import WebsiteSale as Websale

class WebsiteSale(Websale):

@route(DO I REQUIRE TO ENTER ANYTHING HERE???)

def address(self, **kw):

result = super(WebsiteSale, self).address(**kw)

...

WHAT TO ENTER HERE???

...

return result

Thank you once again

Regards

PM

Related Posts Replies Views Activity
1
May 20
5833
3
Dec 19
4552
0
Oct 24
109
1
Jul 24
414
1
Mar 24
926