jQuery(document).ready(function($) {
$('#buttonLoop').on('click', function(e) {
e.preventDefault();
var button = $(this);
var originalText = button.text();
button.text('ŁADOWANIE...');
button.css('cursor', 'progress');
var currentPage = parseInt($('.page-numbers.current').text());
var nextPage = currentPage + 1;
var currentUrl = window.location.href.split('?')[0];
var ajaxUrl = currentUrl + '?product-page=' + nextPage;
$.ajax({
url: ajaxUrl,
type: 'GET',
success: function(response) {
var newProducts = $(response).find('.woocommerce ul.products');
$('.woocommerce ul.products').append(newProducts.html());
var resultCount = $(response).find('.woocommerce-result-count').text();
$('.woocommerce-result-count').text(resultCount);
$('.page-numbers.current').text(nextPage);
button.text('POKAŻ WIĘCEJ');
button.css('cursor', 'pointer');
updateResultCountText();
},
error: function() {
console.log('Błąd podczas ładowania nowych produktów.');
button.text('POKAŻ WIĘCEJ');
button.css('cursor', 'pointer');
}
});
});
function updateResultCountText() {
const element = document.querySelector('#numberLoop .woocommerce-result-count');
if (element) {
let text = element.textContent;
text = text.replace('Wyświetlanie', '');
const hyphenIndex = text.indexOf('–');
if (hyphenIndex !== -1) {
text = text.substring(hyphenIndex + 1).trim();
}
text = text.replace('wyników', 'PRODUKTÓW');
element.textContent = text;
}
}
updateResultCountText();
});