Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/view/frontend/templates/layer/navigation-form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
*/
Expand Down Expand Up @@ -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') {
Expand Down
12 changes: 9 additions & 3 deletions src/view/frontend/templates/layer/view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ $renderFilterButton = $tweakwiseNavigationConfig->isFormFilters();

$hasFilters = count($filters) > 0;
$filtered = count($block->getLayer()->getState()->getFilters());

if ($renderFilterButton) {
$showItemsLabel = __('Show %count% items')->render();
$productCount = $block->getLayer()->getProductCollection()->getSize();
$showItemsText = __('Show %1 items', $productCount)->render();
}
?>

<div class="block filter border border-container bg-container p-2 md:p-0 md:border-0 md:bg-transparent my-6<?=(!$hasFilters ? ' filter-no-options' : '')?>"
Expand Down Expand Up @@ -154,10 +160,10 @@ $filtered = count($block->getLayer()->getState()->getFilters());
<div class="show-items-link">
<button type="button"
class="btn btn-primary btn-block js-btn-filter"
aria-label="<?= $escaper->escapeHtmlAttr(__('Apply filters')) ?>"
data-count-label="<?= $escaper->escapeHtmlAttr($showItemsLabel) ?>"
aria-label="<?= $escaper->escapeHtmlAttr($showItemsText) ?>"
>
<?= $escaper->escapeHtml(__('Show items')); ?>

<?= $escaper->escapeHtml($showItemsText); ?>
</button>
</div>
<?php endif; ?>
Expand Down