Skip to content

Instantly share code, notes, and snippets.

@Sally165
Created April 24, 2023 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sally165/df7109d694c5128ebd2044b293bfcf09 to your computer and use it in GitHub Desktop.
Save Sally165/df7109d694c5128ebd2044b293bfcf09 to your computer and use it in GitHub Desktop.
<!-- PAGEFLY CART HELPER - DO NOT DELETE -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
try {
setTimeout(function () {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function (res) {
$.getJSON('/cart.json').then(cart => {
console.log('cart',cart)
this.$popup = $('.js-popup-cart-ajax');
if(this.$popup.length) {
console.log('do if')
const $content = this.$popup.find('.popup-cart__content'),
$empty = this.$popup.find('.popup-cart__empty'),
$items = this.$popup.find('.popup-cart__items'),
$count = this.$popup.find('[data-js-popup-cart-count]'),
$subtotal = this.$popup.find('[data-js-popup-cart-subtotal]'),
$discounts = this.$popup.find('[data-js-popup-cart-discounts]');
$count.html(theme.strings.general.popups.cart.count.replace('{{ count }}',cart.item_count));
$content[cart.item_count > 0 ? 'removeClass' : 'addClass']('d-none-important');
$empty[cart.item_count > 0 ? 'addClass' : 'removeClass']('d-none-important');
$discounts.html('').addClass('d-none');
if(cart.item_count > 0) {
const $template = $($('#template-cart-ajax')[0].content),
$fragment = $(document.createDocumentFragment());
cart.items.forEach(function (item, i) {
const $item = $template.clone(),
$product = $item.find('.product-cart'),
$image = $item.find('.product-cart__image img'),
$title = $item.find('.product-cart__title a'),
$variant = $item.find('.product-cart__variant'),
$price = $item.find('.product-cart__price-value'),
$quantity = $item.find('.product-cart__quantity'),
$inputQuantity = $item.find('.product-cart__input-quantity'),
$remove = $item.find('.product-cart__remove'),
$links = $item.find('a').not('.product-cart__remove');
$product.attr('data-product-variant-id', item.variant_id);
$product.attr('data-product-cart-line', i + 1);
$links.attr('href', item.url);
$title.html(item.product_title);
$image.attr('src', Shopify.resizeImage(item.image, '120x')).attr('srcset', Shopify.resizeImage(item.image, '120x') + ' 1x, ' + Shopify.resizeImage(item.image, '240x') + ' 2x');
$quantity.html(item.quantity);
$remove.attr('href', '/cart/change?line=' + (i + 1) + '&amp;quantity=0');
if($inputQuantity.length) {
$inputQuantity.val(item.quantity);
}
if(item.variant !== 'Default variant') {
$variant.html(item.variant_title).removeClass('d-none-important');
}
theme.ProductCurrency.setPriceFull($price, item);
$fragment.append($item);
});
$items.html('');
$items.append($fragment);
theme.ProductCurrency.setPrice($subtotal, cart.total_price);
if(cart.cart_level_discount_applications.length) {
const $template_discount = $($('#template-cart-discount')[0].content);
for(let application of cart.cart_level_discount_applications) {
const $item = $template_discount.clone();
$item.find('[data-js-popup-cart-discount-title]').text(application.title);
theme.ProductCurrency.setPrice($item.find('[data-js-popup-cart-discount-price]'), application.total_allocated_amount);
$discounts.append($item);
}
$discounts.removeClass('d-none');
}
theme.ProductCurrency.update();
} else {
$items.add($subtotal).html('');
}
}
$('[data-js-popup-cart-count]').text(cart.item_count)
$('a[href="/cart"][data-js-popup-button="cart"]')[0].click()
const count = cart.item_count;
const countText=$('[data-js-cart-count-desktop]').text()
let findCount
console.log('lenght',count)
if(count<11){
findCount=countText.charAt(parseInt(countText.length)-2)
}else{
findCount=countText.substring(parseInt(countText.length)-3,parseInt(countText.length)-1)
}
$('[data-js-cart-count-mobile]').attr('data-js-cart-count-mobile', count).html(count);
const text= $('[data-js-cart-count-desktop]').text().replace(`${findCount}`,`${cart.item_count}` )
$('[data-js-cart-count-desktop]').text(text)
});
});
}, 2500);
} catch (e) {
console.warn(e);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment