Skip to content

Instantly share code, notes, and snippets.

@hta218
Forked from paul-phan/pagefly-atc-shopify.js
Last active April 17, 2020 03:41
Show Gist options
  • Save hta218/7c05629245de947220bb91954ae6b5b4 to your computer and use it in GitHub Desktop.
Save hta218/7c05629245de947220bb91954ae6b5b4 to your computer and use it in GitHub Desktop.
pagefly-atc-shopify.js
////////////////////////////////////////////////// Basic template
// PageFly ATC Helpers - Do not delete
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
});
});
} catch(e) { console.error("PF Error:: ", e) }
/////// Mosaic Theme Troop Themes
// Keywords: Partial
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
$('.partial--cart.modal').trigger('renderCart');
});
});
} catch(e) { console.error("PF Error:: ", e) }
/////// Minimal theme
// Keywords: Shopify.AjaxifyCat
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
var _config = Shopify.AjaxifyCart.getConfig();
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
if (_config.cartCountSelector && $(_config.cartCountSelector).size()) {
var value = $(_config.cartCountSelector).html() || '0';
$(_config.cartCountSelector).html(value.replace(/[0-9]+/,cart.item_count)).removeClass('hidden-count');
}
if (_config.cartTotalSelector && $(_config.cartTotalSelector).size()) {
if (typeof Currency !== 'undefined' && typeof Currency.moneyFormats !== 'undefined') {
var newCurrency = '';
if ($('[name="currencies"]').size()) {
newCurrency = $('[name="currencies"]').val();
}
else if ($('#currencies span.selected').size()) {
newCurrency = $('#currencies span.selected').attr('data-currency');
}
if (newCurrency) {
$(_config.cartTotalSelector).html('<span class=money>' + Shopify.formatMoney(Currency.convert(cart.total_price, "GBP", newCurrency), Currency.money_format[newCurrency]) + '</span>');
}
else {
$(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, "£{{amount}}"));
}
}
else {
$(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, "£{{amount}}"));
}
};
});
});
});
} catch(e) { console.error("PF Error:: ", e) }
/////// Hardcode Tax, Rounded Prices theme
// Keyword: default.js, updateCartBox
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(p) {
// Handle update cart here
updateCartBox();
$("html, body").animate({ scrollTop: 0 }, "fast");
$('.cart-notification .thumb a').attr('href', p.url)
$('.cart-notification .thumb img').attr('src', p.image)
$('.cart-notification .products-added h3').html(p.product_title.replace(' / Default', '').replace('Default', ''))
if(p.variant_title){
$('.cart-notification .products-added h4').html(p.variant_title.replace(' / Default', '').replace('Default', ''))
}
newprice = p.price;
var subprice = Shopify.formatMoney(newprice, "");
var subHtml = "<span class='price'><span data-price='" + subprice + "'>" + subprice + "</span></span>";
$('.cart-notification .products-added .product-price').html(p.quantity + " x " + subHtml);
round2DecimalMoney();
$('.cart-notification').fadeIn();
setTimeout(function(){ $('.cart-notification').fadeOut(); }, 5000);
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Google tag install theme
// Keywords:
// PageFly ATC Helpers - Do not delete
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
//Get our data
var addedDataJSON = res;
var cartPopupTemplate = [
'<div id="cart-summary-overlay" class="cf">',
'<div class="item">',
'<div class="just-title">'+"Just added:"+'</div>',
'<div class="cols">',
'<div class="img"><img src="[[image_url]]" /></div>',
'<div class="info">',
'<div>[[title]]</div>',
'<div>[[price]]</div>',
'</div>',
'<div class="detail">',
'<div>'+"Qty"+' [[quantity]]</div>',
'<div>'+"Total"+' [[line_price]]</div>',
'</div>',
'</div>',
'</div>',
'<div class="cart">',
'<div>',
"Subtotal"+' <span>[[subtotal]]</span>',
'</div>',
'<div class="cart-text">'+"Excl. shipping"+'&nbsp;</div>',
'<div>',
'<a id="shop-more" class="button altcolour" href="#">'+"Keep Shopping"+'</a>',
'<a class="button to-cart" href="/cart">'+"Cart"+'</a>',
'</div>',
'</div>',
'</div>'
].join('');
var shopifyAjaxCartURL = '/cart.js';
var shopifyAjaxStorePageURL = '/search';
//Get current cart state
$.get(shopifyAjaxCartURL, function(cartData){
var cartDataJSON = $.parseJSON(cartData);
var $form = $("product_form_" + addedDataJSON.product_id);
var addedQty = $form.find('input[name="quantity"]').length ? parseInt($form.find('input[name="quantity"]').val()) : 1;
var addedImage = addedDataJSON.image;
//Now we have all the data, build the shade
var cartShadeHTML = cartPopupTemplate;
cartShadeHTML = cartShadeHTML.replace('[[title]]', addedDataJSON.title);
cartShadeHTML = cartShadeHTML.replace('[[quantity]]', addedQty);
cartShadeHTML = cartShadeHTML.replace('[[image_url]]', addedImage);
cartShadeHTML = cartShadeHTML.replace('[[price]]', Shopify.formatMoney(addedDataJSON.price, theme.money_format));
cartShadeHTML = cartShadeHTML.replace('[[line_price]]', Shopify.formatMoney(addedDataJSON.price * addedQty, theme.money_format));
cartShadeHTML = cartShadeHTML.replace('[[subtotal]]', Shopify.formatMoney(cartDataJSON.total_price, theme.money_format));
var $cartShade = $(cartShadeHTML);
$cartShade.find('#shop-more').bind('click', function(){
$cartShade.animate({top: - $cartShade.outerHeight()}, 500, function(){ $(this).remove(); });
return false;
});
$cartShade.prependTo('body').css('top', - $cartShade.outerHeight()).animate({top: 0}, 500);
//Update currency of amount
Currency.convertAll(shopCurrency, jQuery('[name=currencies]').val());
}, 'html');
//Update header summaries
$.get(shopifyAjaxStorePageURL, function(data){
var cartSummarySelectors = ['#pageheader .cart-summary .cart-count', '#mobile-header .cart-count'];
for(var i=0; i<cartSummarySelectors.length; i++) {
var $newCartObj = $('<div>' + data + '</div>').find(cartSummarySelectors[i]).first();
var $currCart = $(cartSummarySelectors[i]);
$currCart.html($newCartObj.html());
}
});
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Avanue theme
// Keywords: script.js script-initial.js
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function() {
// Handle update cart here
var _config = {
howLongTillBtnReturnsToNormal: 1000,
cartCountSelector: '.cartCountSelector',
cartTotalSelector: '.cartTotalSelector',
addToCartBtnSelector: '[type="submit"]',
shopifyAjaxAddURL: theme.routes_cart_add_url,
shopifyAjaxCartURL: theme.routes_cart_url,
shopifyAjaxCollectionURL: $('#collection_url').attr('href')
};
$.ajax({
type: 'GET',
url: "/",
complete: function (data) {
$('#cart-dropdown-span').html($('#cart-dropdown-span', data.responseText).html());
}
});
$.getJSON(_config.shopifyAjaxCartURL, function(cart) {
if (_config.cartCountSelector && $(_config.cartCountSelector).length) {
var value = $(_config.cartCountSelector).html() || '0';
$(_config.cartCountSelector).html(value.replace(/[0-9]+/,cart.item_count)).removeClass('hidden');
$(_config.cartTotalSelector).html(Shopify.formatMoney(cart.total_price, theme.moneyFormat).replace(/((\,00)|(\.00))$/g, ''));
};
});
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Atlantic Theme
// Keywords: atlantic.js CartMini
// Require: export CartMini instance to window (Example: https://monosnap.com/file/snN2GfZ62VdQxa6TCZJxDZmB7Cpk37)
// window.__minicart = instance;
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
__minicart.refresh();
__minicart.open();
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Installme theme
// Keywords: proper.js bootstrap timber Install Me
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
Shopify.onItemAdded(res);
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Pixel Union theme
// Keywords: pixelunion, ThemeView, CartView, QuickShopView, site.js
// Require: export QuickShopView to window: QuickShopView = window.QuickShopView = ....
// PageFly ATC Helpers - Do not delete
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(data) {
// Handle update cart here
var properties = {};
properties.variant = {};
properties.product = {};
properties.quantity = {};
properties.quantity.added = data.quantity
properties.variant.id = data.id
properties.variant.title = data.variant_title
properties.product.title = data.product_title
properties.product.url = data.url
QuickShopView.prototype.updateMiniCart(data, properties)
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Elessi Theme
// Keywords: elessiShopify, jas_theme
// PageFly ATC Helpers - Do not delete
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
$.get('/cart?view=json', function(data, status) {
/*optional stuff to do after success */
$('.widget_shopping_cart_content').html(data);
}).always(function() {
const cart = res;
var subtotal = parseFloat($('.widget_shopping_cart_body').data('subtotal'));
$(".nathan-cart-subtotal >span").html(elessiShopifyPre.formatMoney(subtotal, nathan_settings.moneyFormat));
$(".cartCount")
.html($('.widget_shopping_cart_body')
.data('count'));
elessiShopify
.favicon_counter(parseInt($('.widget_shopping_cart_body')
.data('count')));
elessiShopify.nanoScroller();
if (nathan_settings.show_multiple_currencies && elessiShopifyPre.StorageCurrency() !== null) {
Currency.convertAll(shopCurrency, elessiShopifyPre.StorageCurrency(), '.widget_shopping_cart_content span.money');
Currency.convertAll(shopCurrency, elessiShopifyPre.StorageCurrency(), '.nathan-cart-subtotal span.money');
}
if (parseInt($('.widget_shopping_cart_body').data('count')) > 0 ) {
elessiShopify.initAddToCart(cart.image,cart.title);
}
});
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Timber theme
// Keywords: Timber, enquirejs, ajaxifyShopify
// Require: export cartUpdateCallback func in ajaxifyShopify module
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
// Handle update cart here
Shopify.getCart(ajaxifyShopify.cartUpdateCallback);
// Or
// ShopifyAPI.getCart(ajaxCart.adjustCartCallback)
// Or
// Shopify.onItemAdded(__pagefly_helper_store__.lastATCResult);
// Or
// Shopify.getCart(cart => {
// document.querySelector("#cart #item_count").textContent = cart.item_count
// })
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Slate Theme
// Keywords: successDrawer, #successDrawer, CartJS, Queue
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
CartJS.getCart({ type: "GET", update: true });
var $body = $("body");
var $successDrawer = $("#successDrawer");
$successDrawer.find(".modal-prod-img").attr("src",'');
var pimg = res.image,
pimg_path = pimg.replace(/(\.[^\.]*$|$)/, '_small' + '$&');
$successDrawer.find(".modal-prod-img").attr("src", pimg_path);
$successDrawer.find(".modal-prod-name").text(res.product_title);
$body.addClass("showOverly");
$successDrawer.fadeIn(500);
drawerTimeout = setTimeout(function(){
$body.removeClass("showOverly");
$successDrawer.fadeOut(200);
}, 8000);
});
});
} catch(e) { console.error("PF Error:: ", e) }
////// Theme theme (A theme that named "Theme" !!)
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
t.showModal(".ajax-success-modal");
$(".ajax-success-modal").find(".ajax-product-image").attr("src", res.featured_image.url);
$(".ajax-success-modal").find(".added-to-wishlist").hide();
$(".ajax-success-modal").find(".added-to-cart").show();
$(".ajax-success-modal").find(".ajax-product-title").text(res.title);
$(".ajax-success-modal").find(".ajax_price").text(res.price);
$(".ajax-success-modal").find(".ajax_qty").text(res.quantity);
t.updateDropdownCart();
})
});
} catch(e) { console.error('PF error:: ', e) }
///////// Venue theme
// Require: Remember to export 'buildCart' func in 'ajaxCart' module
try {
window.addEventListener("load", function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function() {
ShopifyAPI.getCart(function(cart) {
ajaxCart.buildCart(cart);
$(document.body).trigger('afterAddItem.ajaxCart');
})
})
});
} catch(err) {
console.error("PF: err", err);
}
///////// Turbo theme
// Require: Remenber export the refreshCart function to window
try {
window.addEventListener('load', function() {
window.__pagefly_helper_store__ && window.__pagefly_helper_store__.subscribe(function(res) {
$.ajax({
url: '/cart.js',
dataType: "json",
cache: false,
success: function(cart) {
//block scrolling on mobile
if ($(window).width() <= 798) {
$('body').addClass('blocked-scroll');
}
window.refreshCart(cart);
if($('#header').is(':visible')) {
$('#header .cart_container').addClass('active_link');
} else if ($('.sticky_nav--stick').length) {
$('.sticky_nav .cart_container').addClass('active_link');
} else {
$('.top_bar .cart_container').addClass('active_link');
}
}
});
})
});
} catch(e) { console.error("PF:: ", e) }
// Nathan theme | Gecko theme
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
$.get("/cart?view=json", function(data, status) {
/*optional stuff to do after success */
$(".widget_shopping_cart_content").html(data);
}).always(function() {
var subtotal = parseFloat(
$(".widget_shopping_cart_body").data("subtotal")
);
$(".gecko-cart-subtotal >span").html(
elessiShopifyPre.formatMoney(subtotal, nathan_settings.moneyFormat)
);
$(".cartCount").html($(".widget_shopping_cart_body").data("count"));
elessiShopify.favicon_counter(
parseInt($(".widget_shopping_cart_body").data("count"))
);
elessiShopify.nanoScroller();
if (
nathan_settings.show_multiple_currencies &&
elessiShopifyPre.StorageCurrency() !== null
) {
Currency.convertAll(
shopCurrency,
elessiShopifyPre.StorageCurrency(),
".widget_shopping_cart_content span.money"
);
Currency.convertAll(
shopCurrency,
elessiShopifyPre.StorageCurrency(),
".gecko-cart-subtotal span.money"
);
}
elessiShopify.initAddToCart(
__pagefly_helper_store__.lastATCResult.product_id
);
});
});
}, 1500);
} catch (e) {
console.warn(e);
}
// Narative theme
// Require: need to put the cart on to window
var cart = window.cart = {}
// then add this code to the bottom of js file.
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
window.cart &&
window.cart._promiseChange({
url: "/cart.json",
dataType: "json"
});
window.theme.sections.instances[0]._openCartDrawer(new Event("click"));
});
}, 1500);
} catch (e) {
console.warn(e);
}
// Some unnamed theme.
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
theme.ProductPage._updateCartCount();
});
}, 1500);
} catch (e) {
console.warn(e);
}
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
document.body.dispatchEvent(
new CustomEvent("product:added", {
bubbles: true,
detail: {
variant: __pagefly_helper_store__.lastATCResult.variant_id,
quantity: __pagefly_helper_store__.lastATCResult.quantity
}
})
);
});
}, 1500);
} catch (e) {
console.warn(e);
}
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
Shopify.getCart(function(cart) {
Shopify.updateCartInfo(cart, ".cart-info .cart-content");
});
});
}, 1500);
} catch (e) {
console.warn(e);
}
// Fastor theme
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
roar.updateCartSidebar();
});
}, 1500);
} catch (e) {
console.warn(e);
}
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
Shopify.onItemAdded(__pagefly_helper_store__.lastATCResult)
});
}, 1500);
} catch (e) {
console.warn(e);
}
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
$.ajax({
url: "/cart.js",
dataType: "json",
cache: false,
success: function success(cart) {
refreshCart(cart);
window.setTimeout(function() {
$.fancybox.close();
$(".cart-button").click();
}, 500);
}
});
});
}, 1500);
} catch (e) {
console.warn(e);
}
// Booster theme
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
$.getJSON("/cart.json", function(cart) {
var total_price = cart.total_price;
$("#CartCost").text(
theme.Currency.formatMoney(total_price, theme.moneyFormat)
);
$("#CartCount > span").text(cart.item_count);
});
});
}, 1500);
} catch (e) {
console.warn(e);
}
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
Shopify.theme.jsAjaxCart.showDrawer();
Shopify.theme.jsAjaxCart.updateView();
});
}, 1500);
} catch (e) {
console.warn(e);
}
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
window.__pagefly_help.getCart(console.log);
window.__pagefly_help.notification(
window.__pagefly_helper_store__.lastATCResult
);
});
}, 1500);
} catch (e) {
console.warn(e);
}
// Trademark
$.getJSON('/cart.js', cart => {
$(document).trigger('theme:cart:updated', [cart, true, true]);
})
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
roar.updateCart(__pagefly_helper_store__.lastATCResult, true);
});
}, 1500);
} catch (e) {
console.warn(e);
}
/////// District Theme, Style Hatch Inc.
// Require: export showCartPreview func
try {
setTimeout(function() {
window.__pagefly_helper_store__ &&
window.__pagefly_helper_store__.subscribe(function(c) {
$.getJSON("/cart", function(cart) {
StyleHatch.AjaxCart.updateCartButton(cart);
StyleHatch.AjaxCart.showCartPreview(
JSON.stringify(__pagefly_helper_store__.lastATCResult),
cart
);
});
});
}, 1500);
} catch (e) {
console.warn(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment