[IMP] website_sale: show shop category final URL in sitemap

Before this commit, when an user would visit a subcategory in the ecommerce, he
would see a slugified URL, containing all the category hierarchy, which is good
for SEO purpose.

For example, if `Wheel` is a child of `Car` which is a child of `Vehicule`,
then the URL would be `/shop/category/vehicule-car-wheel-3`.

But on the sitemap, the parents would not be prefixed to the URL, thus the
sitemap would simply show `<loc>/shop/category/wheel-3</loc>`.

This commit ensure the category parents are added to the final URL of the
sitemap, as in the ecommerce.

task-1889972

closes odoo/odoo#34073

Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
This commit is contained in:
Romain Derie
2019-06-25 09:02:05 +00:00
parent 9bc974c2f8
commit b2eceae7c9
3 changed files with 42 additions and 3 deletions
+16 -3
View File
@@ -10,6 +10,7 @@ from odoo.addons.base.models.ir_qweb_fields import nl2br
from odoo.addons.http_routing.models.ir_http import slug
from odoo.addons.payment.controllers.portal import PaymentProcessing
from odoo.addons.website.controllers.main import QueryURL
from odoo.addons.website.models.ir_http import sitemap_qs2dom
from odoo.exceptions import ValidationError
from odoo.addons.website.controllers.main import Website
from odoo.addons.website_form.controllers.main import WebsiteForm
@@ -197,12 +198,24 @@ class WebsiteSale(http.Controller):
return expression.AND(domains)
def sitemap_shop(env, rule, qs):
if not qs or qs.lower() in '/shop':
yield {'loc': '/shop'}
Category = env['product.public.category']
dom = sitemap_qs2dom(qs, '/shop/category', Category._rec_name)
dom += env['website'].get_current_website().website_domain()
for cat in Category.search(dom):
loc = '/shop/category/%s' % slug(cat)
if not qs or qs.lower() in loc:
yield {'loc': loc}
@http.route([
'''/shop''',
'''/shop/page/<int:page>''',
'''/shop/category/<model("product.public.category", "[('website_id', 'in', (False, current_website_id))]"):category>''',
'''/shop/category/<model("product.public.category", "[('website_id', 'in', (False, current_website_id))]"):category>/page/<int:page>'''
], type='http', auth="public", website=True)
'''/shop/category/<model("product.public.category"):category>''',
'''/shop/category/<model("product.public.category"):category>/page/<int:page>'''
], type='http', auth="public", website=True, sitemap=sitemap_shop)
def shop(self, page=0, category=None, search='', ppg=False, **post):
add_qty = int(post.get('add_qty', 1))
if category:
+1
View File
@@ -1,5 +1,6 @@
from . import test_customize
from . import test_sale_process
from . import test_sitemap
from . import test_website_sale_cart_recovery
from . import test_website_sale_mail
from . import test_website_sale_pricelist
+25
View File
@@ -0,0 +1,25 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import HttpCase, tagged
@tagged('post_install', '-at_install')
class TestSitemap(HttpCase):
def setUp(self):
super(TestSitemap, self).setUp()
self.cats = self.env['product.public.category'].create([{
'name': 'Level 0',
}, {
'name': 'Level 1',
}, {
'name': 'Level 2',
}])
self.cats[2].parent_id = self.cats[1].id
self.cats[1].parent_id = self.cats[0].id
def test_01_shop_route_sitemap(self):
resp = self.url_open('/sitemap.xml')
level2_url = '/shop/category/level-0-level-1-level-2-%s' % self.cats[2].id
self.assertTrue(level2_url in resp.text, "Category entry in sitemap should be prefixed by its parent hierarchy.")