From d46d352828f12654ff03f275c76ea05e5ab9ccb5 Mon Sep 17 00:00:00 2001 From: ahuininga-orisha <> Date: Thu, 7 May 2026 12:56:58 +0200 Subject: [PATCH 1/2] feat: Add product count fetch for filter buttons --- .../templates/layer/navigation-form.phtml | 86 +++++++++++++++++++ src/view/frontend/templates/layer/view.phtml | 13 ++- 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/src/view/frontend/templates/layer/navigation-form.phtml b/src/view/frontend/templates/layer/navigation-form.phtml index 8c9331d..cc66887 100644 --- a/src/view/frontend/templates/layer/navigation-form.phtml +++ b/src/view/frontend/templates/layer/navigation-form.phtml @@ -23,12 +23,14 @@ use Magento\Framework\View\Element\Template; formFilters: false, seoEnabled: false, ajaxEndpoint: '/tweakwise/ajax/navigation', + countEndpoint: '/tweakwise/ajax/productcount', filterFormSelector: '#facet-filter-form', filterSelector: '#facet-filter-form', productListSelector: '.products.wrapper', toolbarSelector: '.toolbar.toolbar-products', isLoading: false, currentXhr: null, + countFetchController: null, fetchController: null, ajaxCache: true, urlStrategy: null, @@ -42,6 +44,7 @@ use Magento\Framework\View\Element\Template; if (filterOptions) { this.ajaxEndpoint = filterOptions.ajaxEndpoint; + this.countEndpoint = filterOptions.countEndpoint || this.countEndpoint; this.ajaxFilters = filterOptions.ajaxFilters; this.formFilters = filterOptions.formFilters; this.seoEnabled = filterOptions.seoEnabled; @@ -63,6 +66,10 @@ use Magento\Framework\View\Element\Template; this.pmListReload(pmOptions.cookieName); } } + + if (this.formFilters) { + this.bindCountUpdateEvents(); + } }, bindFilterClickEvents() { const filterForm = document.querySelector(this.filterFormSelector); @@ -83,6 +90,81 @@ use Magento\Framework\View\Element\Template; } }, + /** + * Bind change events on filter inputs to update the "Show X items" button count. + * Only active when formFilters is enabled. + */ + bindCountUpdateEvents() { + const filterForm = document.querySelector(this.filterFormSelector); + + if (!filterForm) { + return; + } + + filterForm.addEventListener('change', (event) => { + if (event.target.classList.contains('js-skip-submit')) { + return; + } + + this.fetchAndUpdateCount(); + }); + }, + + /** + * Fire a lightweight AJAX request to retrieve the product count for the current + * filter selection and update all "Show X items" buttons in the filter block. + */ + fetchAndUpdateCount() { + if (this.countFetchController) { + this.countFetchController.abort(); + } + + const filterParams = this.getFilterParameters(); + let fetchUrl = this.countEndpoint; + + if (filterParams) { + fetchUrl = `${fetchUrl}?${filterParams}`; + } + + this.countFetchController = new AbortController(); + + fetch(fetchUrl, { signal: this.countFetchController.signal, cache: 'no-cache' }) + .then(response => response.json()) + .then(response => { + if (typeof response.product_count !== 'undefined') { + this.updateFilterButtonCount(response.product_count); + } + }) + .catch((error) => { + if (error.name !== 'AbortError') { + console.error('Error fetching product count:', error); + } + }); + }, + + /** + * Update the label of all "Show X items" buttons with the given product count. + * + * @param {number} count + */ + updateFilterButtonCount(count) { + const filterForm = document.querySelector(this.filterFormSelector); + + if (!filterForm) { + return; + } + + filterForm.querySelectorAll('.js-btn-filter').forEach((button) => { + const label = button.dataset.countLabel || ''; + const text = label.replace('%count%', count); + + if (text) { + button.textContent = text; + button.setAttribute('aria-label', text); + } + }); + }, + /** * Init list reload for Personal Merchandising Cookie */ @@ -246,6 +328,10 @@ use Magento\Framework\View\Element\Template; .then(response => { this.ajaxUpdateBlocks(response.html); this.ajaxUpdateState(response); + + if (typeof response.product_count !== 'undefined') { + this.updateFilterButtonCount(response.product_count); + } }) .catch((error) => { if (error.name !== 'AbortError') { diff --git a/src/view/frontend/templates/layer/view.phtml b/src/view/frontend/templates/layer/view.phtml index 043228a..caa4fbc 100644 --- a/src/view/frontend/templates/layer/view.phtml +++ b/src/view/frontend/templates/layer/view.phtml @@ -30,6 +30,13 @@ $renderFilterButton = $tweakwiseNavigationConfig->isFormFilters(); $hasFilters = count($filters) > 0; $filtered = count($block->getLayer()->getState()->getFilters()); + +if ($renderFilterButton) { + /** @var \Magento\Framework\Phrase $showItemsLabel */ + $showItemsLabel = __('Show %count% items'); + $productCount = $block->getLayer()->getProductCollection()->getSize(); + $showItemsText = __('Show %1 items', $productCount); +} ?>