| Server IP : 104.171.130.249 / Your IP : 216.73.216.146 Web Server : nginx/1.25.5 System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/html/wp-content/themes/matreshka/assets/js/ |
Upload File : |
/**
* Add to Cart functionality for product cards
*/
jQuery(document).ready(function($) {
'use strict';
var addToCartQueue = [];
var isProcessingAddToCart = false;
var pendingProductIds = {};
function getCartUrl(response) {
return response && response.data && response.data.cart_url ? response.data.cart_url : '/cart/';
}
function createViewCartButton(cartUrl) {
return $('<a>')
.attr('href', cartUrl)
.addClass('btn btn-secondary montserrat-bold view-cart-btn')
.text('Перейти в корзину');
}
function getProductButtons(productId) {
return $('.add-to-cart-btn').filter(function() {
return String($(this).data('product_id')) === String(productId);
});
}
function getProductButtonsWithFallback(productId, $fallbackButton) {
var $buttons = getProductButtons(productId);
if (!$buttons.length && $fallbackButton && $fallbackButton.length) {
$buttons = $fallbackButton;
}
return $buttons;
}
function replaceProductButtons(productId, cartUrl, $fallbackButton) {
var $buttons = getProductButtonsWithFallback(productId, $fallbackButton);
$buttons.each(function() {
$(this).replaceWith(createViewCartButton(cartUrl));
});
}
function updateCartBadge(cartCount, cartTotalHtml, allowDecrease) {
$('.cart-count, .header-cart-count').text(cartCount);
var $cartBadge = $('.cart-count-badge');
if (cartCount > 0) {
if ($cartBadge.length) {
$cartBadge.text(cartCount);
} else {
var $badge = $('<span>')
.addClass('cart-count-badge montserrat-semibold')
.text(cartCount);
$('.cart-icon-wrapper').append($badge);
}
} else {
$cartBadge.remove();
}
$(document.body).trigger('matreshka_cart_count_updated', [{
cart_count: cartCount,
cart_total_html: cartTotalHtml || null,
allow_decrease: allowDecrease !== false
}]);
}
function restoreButton($button, originalText) {
$button.removeClass('loading error')
.prop('disabled', false)
.text(originalText);
}
function getButtonQuantity($button) {
var $form = $button.closest('form.cart');
var $input = $form.find('input.qty, input.qty-input, input[name="quantity"]').first();
var quantity = parseFloat(String($input.val() || '').replace(',', '.'));
if (isNaN(quantity) || quantity <= 0) {
quantity = parseFloat($button.data('quantity'));
}
return !isNaN(quantity) && quantity > 0 ? quantity : 1;
}
function setProductButtonsLoading(productId, $fallbackButton) {
getProductButtonsWithFallback(productId, $fallbackButton).each(function() {
var $button = $(this);
if (!$button.data('original_text')) {
$button.data('original_text', $button.text().trim());
}
$button.prop('disabled', true)
.addClass('loading')
.text('Добавляется...');
});
}
function restoreProductButtons(productId, $fallbackButton, originalText) {
getProductButtonsWithFallback(productId, $fallbackButton).each(function() {
var $button = $(this);
var buttonText = $button.data('original_text') || originalText;
restoreButton($button, buttonText);
$button.removeData('original_text');
});
}
function showButtonError(item) {
var $buttons = getProductButtonsWithFallback(item.productId, item.$button);
$buttons.removeClass('loading')
.addClass('error')
.prop('disabled', false)
.text('Ошибка');
setTimeout(function() {
restoreProductButtons(item.productId, item.$button, item.originalText);
}, 2000);
}
function processAddToCartQueue() {
if (isProcessingAddToCart || !addToCartQueue.length) {
return;
}
isProcessingAddToCart = true;
var item = addToCartQueue.shift();
var $button = item.$button;
$.ajax({
url: matreshka_ajax.ajax_url,
type: 'POST',
data: {
action: 'matreshka_add_to_cart',
product_id: item.productId,
quantity: item.quantity,
nonce: matreshka_ajax.nonce
},
success: function(response) {
if (response.success) {
if (response.data.cart_count !== undefined) {
updateCartBadge(response.data.cart_count, response.data.cart_total_html, false);
}
replaceProductButtons(item.productId, getCartUrl(response), $button);
$(document.body).trigger('added_to_cart', [response.data.fragments, response.data.cart_hash, $button]);
} else {
showButtonError(item);
if (response.data && response.data.message) {
console.error('Add to cart error:', response.data.message);
}
}
},
error: function(xhr, status, error) {
console.error('AJAX error:', error);
showButtonError(item);
},
complete: function() {
delete pendingProductIds[item.productId];
isProcessingAddToCart = false;
processAddToCartQueue();
}
});
}
// Check if products are already in cart on page load
function checkProductsInCart() {
$('.add-to-cart-btn').each(function() {
var $button = $(this);
var productId = $button.data('product_id');
if (!productId) {
return;
}
// AJAX request to check if product is in cart
$.ajax({
url: matreshka_ajax.ajax_url,
type: 'POST',
data: {
action: 'matreshka_is_product_in_cart',
product_id: productId,
nonce: matreshka_ajax.nonce
},
success: function(response) {
if (response.success && response.data.in_cart) {
replaceProductButtons(productId, getCartUrl(response), $button);
}
}
});
});
}
// Run check on page load
checkProductsInCart();
// Handle add to cart button clicks
$(document).on('click', '.add-to-cart-btn', function(e) {
e.preventDefault();
var $button = $(this);
var productId = $button.data('product_id');
if (!productId) {
console.error('Product ID not found');
return;
}
if (pendingProductIds[productId]) {
return;
}
var originalText = $button.text().trim();
pendingProductIds[productId] = true;
setProductButtonsLoading(productId, $button);
addToCartQueue.push({
$button: $button,
productId: productId,
quantity: getButtonQuantity($button),
originalText: originalText
});
processAddToCartQueue();
});
// Update cart badge on WooCommerce events
$(document.body).on('removed_from_cart updated_wc_div', function() {
// Make AJAX request to get current cart count
$.ajax({
url: matreshka_ajax.ajax_url,
type: 'POST',
data: {
action: 'matreshka_get_cart_count',
nonce: matreshka_ajax.nonce
},
success: function(response) {
if (response.success && response.data.cart_count !== undefined) {
updateCartBadge(response.data.cart_count, response.data.cart_total_html, true);
}
}
});
});
});