[PERF] website_sale_wishlist: check wishlist content once

For each product of the shop page (if the template was enabled ofc),
a call to `_is_in_wishlist` would be made, triggering one query to find
the wishlist belonging to the current customer.

This is now done only once in the controller to load all the wishlisted
products.

This code was previously taking up to 15% of the loading time, even when
no wishlist were ever created on the database.
Now, in the best case (no wishlist, standard situation), the time taken
went down to 0.05% of the loading time, gaining > 100 ms of loading time
(when website_sale_wishlist is installed and the template is not disabled).

closes odoo/odoo#142860

Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
Victor Feyens
2023-11-28 21:20:56 +00:00
parent 29b6b851a6
commit 070e78a726
2 changed files with 8 additions and 2 deletions
@@ -8,6 +8,12 @@ from odoo.addons.website_sale.controllers.main import WebsiteSale
class WebsiteSaleWishlist(WebsiteSale):
def _get_additional_shop_values(self, values):
""" Hook to update values used for rendering website_sale.products template """
vals = super()._get_additional_shop_values(values)
vals['products_in_wishlist'] = request.env['product.wishlist'].current().product_id.product_tmpl_id
return vals
@route(['/shop/wishlist/add'], type='json', auth="public", website=True)
def add_to_wishlist(self, product_id, **kw):
website = request.website
@@ -3,8 +3,8 @@
<template id="add_to_wishlist" inherit_id="website_sale.products_item" name="Wishlist Button" priority="20">
<xpath expr="//div[hasclass('o_wsale_product_btn')]" position="inside">
<t t-set="in_wish" t-value="product._is_in_wishlist()"/>
<t t-set="product_variant_id" t-value="product._get_first_possible_variant_id()"/>
<t t-set="in_wish" t-value="product in products_in_wishlist"/>
<t t-set="product_variant_id" t-value="in_wish or product._get_first_possible_variant_id()"/>
<button t-if="product_variant_id"
type="button"
role="button"