Skip to content

Instantly share code, notes, and snippets.

@j7u7l7s
Created July 9, 2018 21:23
Show Gist options
  • Save j7u7l7s/3f2592662d2cd51e6901c2dc7a15bd1f to your computer and use it in GitHub Desktop.
Save j7u7l7s/3f2592662d2cd51e6901c2dc7a15bd1f to your computer and use it in GitHub Desktop.
<script>
$(document).on('click',"#AddToCart-product-template, .buy-bar [name=add]", function() {
console.log('added');
var cartString = $('#CartCount').html();
var miniCounter = parseInt(cartString);
var qty = 1;
var id;
{% if product.variants.size > 1 %}
id = $('.variant-select-for-id option:selected').data('variant');
console.log(id, 'true');
{% endif %}
{% if product.variants.size == 1 %}
id = $('.single-variant-title').data('variant');
console.log(id, 'false');
{% endif %}
addItem=function(qty,id,callback) {
var params = {quantity:qty,id:id};
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: params,
success: function(){
console.log('added');
$.ajax({
url:'https://stitch-golf-2.myshopify.com',
type:'GET',
success: function(data){
console.log('cart data fetched');
if (window.miniCounter > 0){
var cartTotal = $(data).find('.mini_cart .saso-cart-original-total');
var cartItems = $(data).find('.mini_cart .cart-list');
var cartCounter = $(data).find('#CartCount');
var cartCost = $(data).find('#CartCost');
var shippingMsg = $(data).find('.shipping-savings-message');
$('#CartCount').replaceWith(cartCounter);
$('#CartCost').replaceWith(cartCost);
console.log(cartTotal);
console.log(cartItems);
$('.mini_cart .saso-cart-original-total').replaceWith(cartTotal);
$('.mini_cart .cart-list').replaceWith(cartItems);
$('.mini_cart .shipping-savings-message').replaceWith(shippingMsg);
console.log('old cart replaced ');
$('.mini_cart').show();
}
else {
var cartCounter = $(data).find('#CartCount');
var cartCost = $(data).find('#CartCost');
$('#CartCount').replaceWith(cartCounter);
$('#CartCost').replaceWith(cartCost);
var cartNew = $(data).find('.mini_cart');
$('.mini_cart').replaceWith(cartNew);
console.log('old cart replaced empty');
$('.mini_cart').show();
}
}
});
},
error: function(){}
});
}
//Example :ads
addItem(qty,id,function(){
console.log('done');
});
});
// AddtoCart with product Customizer
$(document).on('click',"#AddToCartCustom, #AddToCartCustom-buyBar", function(){
console.log('custom Add');
var cartString = $('#CartCount').html();
var miniCounter = parseInt(cartString);
var qty = 1;
var id;
{% if product.variants.size > 1 %}
id = $('.variant-select-for-id option:selected').data('variant');
console.log(id, 'true');
{% endif %}
{% if product.variants.size == 1 %}
id = $('.single-variant-title').data('variant');
console.log(id, 'false');
{% endif %}
var initials = $('div[data-option-name=initials] input').val();
var initDirect = $('div[data-option-name=initial-direction] select').val();
console.log( initials);
console.log( initDirect);
var properties = {
"Initial Direction:": initDirect,
"Initials": initials
}
addItem=function(qty,id,properties,callback) {
var params = {quantity:qty,id:id, properties:properties};
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: params,
success: function(){
console.log('custom added');
$.ajax({
url:'https://stitch-golf-2.myshopify.com',
type:'GET',
success: function(data){
console.log('cart data fetched');
if (window.miniCounter > 0){
var cartTotal = $(data).find('.mini_cart .saso-cart-original-total');
var cartItems = $(data).find('.mini_cart .cart-list');
var cartCounter = $(data).find('#CartCount');
var cartCost = $(data).find('#CartCost');
var shippingMsg = $(data).find('.shipping-savings-message');
$('#CartCount').replaceWith(cartCounter);
$('#CartCost').replaceWith(cartCost);
console.log(cartTotal);
console.log(cartItems);
$('.mini_cart .saso-cart-original-total').replaceWith(cartTotal);
$('.mini_cart .cart-list').replaceWith(cartItems);
$('.mini_cart .shipping-savings-message').replaceWith(shippingMsg);
console.log('old cart replaced ');
$('.mini_cart').show();
}
else {
var cartCounter = $(data).find('#CartCount');
var cartCost = $(data).find('#CartCost');
$('#CartCount').replaceWith(cartCounter);
$('#CartCost').replaceWith(cartCost);
var cartNew = $(data).find('.mini_cart');
$('.mini_cart').replaceWith(cartNew);
console.log('old cart replaced empty');
$('.mini_cart').show();
}
}
});
},
error: function(){}
});
}
//Example :ads
addItem(qty,id,properties,function(){
console.log('done');
});
});
$('#SingleOptionSelector-0').on('change', function() {
$( ".buy-bar-watch" ).html(function() {
});
})
$( ".buy-bar-watch" ).html(function() {
var select = $( "#SingleOptionSelector-0" ).val();
});
$(document).on('click','#CartButton' ,function(){
event.preventDefault();
$('.mini_cart').show();
});
$(document).on('click', '#close_cart',function(){
$('.mini_cart').hide();
});
$(document).on('click','.search-click',function(){
$('.util .left-wrapper a.search').trigger('click');
});
/* Remove From Cart*/
$(document).on('click','.cart-remove-link',function(){
var id = $(this).data('cart-variant');
console.log(id);
$.post('/cart/change.js', {quantity: 0, id: id,
success: function(){
console.log('changed');
$.ajax({
url:'https://stitch-golf-2.myshopify.com',
type:'GET',
success: function(data){
var miniCounter = $('#CartCount').val();
var cartTotal = $(data).find('.mini_cart .subtotal');
var cartItems = $(data).find('.mini_cart .cart-items');
var cartCounter = $(data).find('#CartCount');
var cartCost = $(data).find('#CartCost');
var shippingMsg = $(data).find('.shipping-savings-message');
$('#CartCount').replaceWith(cartCounter);
$('#CartCost').replaceWith(cartCost);
if (miniCounter > 0){
$('.mini_cart .subtotal').replaceWith(cartTotal);
$('.mini_cart .cart-items').replaceWith(cartItems);
$('.mini_cart .shipping-savings-message').replaceWith(shippingMsg);
console.log('success');
}
else
{
var cartNew = $(data).find('.mini_cart');
$('.mini_cart').replaceWith(cartNew);
}
$('.mini_cart').show();
}
});
}})
});
// update cart quantity
$(document).on('input','.mini_cart .quantity-select input[type=number]',function(){
var thisItem = $(this);
$(this).trigger('blur');
console.log('quant changed');
var id = $(this).data('cart-variant');
console.log(id);
var quantity = $(this).val();
console.log(quantity);
$.post('/cart/change.js', {quantity: quantity , id: id,
success: function(){
console.log(quantity, id);
setTimeout(function(){
$.ajax({
url:'https://stitch-golf-2.myshopify.com',
type:'GET',
success: function(data){
var cartTotal = $(data).find('.mini_cart .subtotal');
var cartCounter = $(data).find('#CartCount');
var cartCost = $(data).find('#CartCost');
var shippingMsg = $(data).find('.shipping-savings-message');
var cartItems = $(data).find('.mini_cart .cart-items');
$('#CartCount').replaceWith(cartCounter);
$('#CartCost').replaceWith(cartCost);
$('.mini_cart .cart-items').replaceWith(cartItems);
$('.mini_cart .subtotal').replaceWith(cartTotal);
$('.mini_cart .shipping-savings-message').replaceWith(shippingMsg);
console.log('success');
$('.mini_cart').show();
}
}); }, 1000);
}});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment