[FIX] website_sale_aplicoop: prevent grid destruction on event listener attachment

The _attachEventListeners() function was cloning the products-grid element
without its children (cloneNode(false)) to remove duplicate event listeners.
This destroyed all loaded products every time the function was called.

Solution: Use a flag (_delegationListenersAttached) to prevent adding
duplicate event listeners instead of cloning and replacing the grid node.

This fixes the issue where products would disappear ~1-2 seconds after
page load.
This commit is contained in:
snt 2026-02-18 16:53:27 +01:00
parent b15e9bc977
commit b07b7dc671
6 changed files with 521 additions and 218 deletions

View file

@ -1441,12 +1441,17 @@ class AplicoopWebsiteSale(WebsiteSale):
all_products = group_order._get_products_for_group_order(group_order.id)
filtered_products = all_products
# Apply search
# Apply search filter (only if search_query is not empty)
if search_query:
_logger.info("load_products_ajax: Applying search filter: %s", search_query)
filtered_products = filtered_products.filtered(
lambda p: search_query.lower() in p.name.lower()
or search_query.lower() in (p.description or "").lower()
)
_logger.info(
"load_products_ajax: After search filter: %d products",
len(filtered_products),
)
# Apply category filter
if category_filter != "0":
@ -1455,6 +1460,11 @@ class AplicoopWebsiteSale(WebsiteSale):
selected_category = request.env["product.category"].browse(category_id)
if selected_category.exists():
_logger.info(
"load_products_ajax: Applying category filter: %d (%s)",
category_id,
selected_category.name,
)
all_category_ids = [category_id]
def get_all_children(category):
@ -1489,6 +1499,10 @@ class AplicoopWebsiteSale(WebsiteSale):
# Preserve search filter by using intersection
filtered_products = filtered_products & cat_filtered
_logger.info(
"load_products_ajax: After category filter: %d products",
len(filtered_products),
)
except (ValueError, TypeError) as e:
_logger.warning(
"load_products_ajax: Invalid category filter: %s", str(e)
@ -1500,6 +1514,16 @@ class AplicoopWebsiteSale(WebsiteSale):
products_page = filtered_products[offset : offset + per_page]
has_next = offset + per_page < total_products
_logger.info(
"load_products_ajax: Pagination - page=%d, offset=%d, per_page=%d, "
"total=%d, has_next=%s",
page,
offset,
per_page,
total_products,
has_next,
)
# Get prices
pricelist = self._resolve_pricelist()
product_price_info = {}