| 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 : |
/**
* Floating Cart Button — shows when header cart icon scrolls out of view.
* Updates count + total on add-to-cart and cart changes.
*/
(function ($) {
'use strict';
var $btn = $('#matreshka-floating-cart');
var $badge = $btn.find('.mfc__badge');
var $total = $btn.find('.mfc__total');
var $headerCart = $('.acounts-icons .cart-icon-wrapper');
var visible = false;
var lastCartCount = parseInt($badge.attr('data-count'), 10) || 0;
if (!$btn.length) return;
/* ── Intersection Observer: watch header cart icon ── */
function initObserver() {
if (!$headerCart.length || !('IntersectionObserver' in window)) {
// Fallback: show when scrolled past 300px
$(window).on('scroll.mfc', throttle(function () {
toggle($(window).scrollTop() > 300);
}, 100));
return;
}
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
// Show floating btn when header cart is NOT visible AND cart has items
toggle(!entry.isIntersecting);
});
}, { threshold: 0 });
observer.observe($headerCart[0]);
}
/* ── Show / Hide ── */
function toggle(shouldShow) {
var count = parseInt($badge.attr('data-count'), 10) || 0;
var show = shouldShow && count > 0;
if (show === visible) return;
visible = show;
if (show) {
$btn.css('display', 'flex').addClass('mfc--visible');
} else {
$btn.removeClass('mfc--visible');
setTimeout(function () {
if (!visible) $btn.css('display', 'none');
}, 300);
}
}
/* ── Update data ── */
function updateCart(count, totalHtml, options) {
options = options || {};
count = parseInt(count, 10) || 0;
if (options.allowDecrease === false && count < lastCartCount) {
return;
}
lastCartCount = count;
$badge.text(count).attr('data-count', count);
if (totalHtml) {
$total.html(totalHtml);
}
// Also update header badge
var $headerBadge = $('.cart-count-badge');
if (count > 0) {
if ($headerBadge.length) {
$headerBadge.text(count);
}
}
// Pulse animation
$btn.addClass('mfc--pulse');
setTimeout(function () { $btn.removeClass('mfc--pulse'); }, 400);
// Re-evaluate visibility (hide if empty)
if (count <= 0) {
toggle(false);
}
}
$(document.body).on('matreshka_cart_count_updated', function (event, data) {
if (!data || data.cart_count === undefined) {
return;
}
updateCart(data.cart_count, data.cart_total_html, {
allowDecrease: data.allow_decrease !== false,
});
});
/* ── Listen for add-to-cart AJAX responses ── */
$(document).ajaxComplete(function (event, xhr, settings) {
if (!settings || !settings.data) return;
var data = typeof settings.data === 'string' ? settings.data : '';
// Match our custom AJAX actions
var isAddToCart = data.indexOf('matreshka_add_to_cart') !== -1;
if (!isAddToCart &&
data.indexOf('matreshka_remove_cart_item') === -1 &&
data.indexOf('matreshka_update_cart_item') === -1) {
return;
}
try {
var response = JSON.parse(xhr.responseText);
if (response.success && response.data) {
var c = response.data.cart_count;
var t = response.data.cart_total_html || null;
if (c !== undefined) {
updateCart(c, t, { allowDecrease: !isAddToCart });
}
}
} catch (e) {}
});
/* ── Also listen for cart removals ── */
$(document.body).on('removed_from_cart', function () {
// Fetch fresh data
if (typeof matreshka_ajax !== 'undefined') {
$.post(matreshka_ajax.ajax_url, {
action: 'matreshka_get_cart_count',
nonce: matreshka_ajax.nonce,
}, function (response) {
if (response.success && response.data) {
updateCart(response.data.cart_count, response.data.cart_total_html, { allowDecrease: true });
}
});
}
});
/* ── Throttle helper ── */
function throttle(fn, wait) {
var last = 0;
return function () {
var now = Date.now();
if (now - last >= wait) {
last = now;
fn.apply(this, arguments);
}
};
}
/* ── Init ── */
initObserver();
})(jQuery);