Created
May 21, 2017 11:53
-
-
Save kenanfallon/fcdcd19991d10f28cf557e220250f1eb to your computer and use it in GitHub Desktop.
WooCommerce AJAX Add to Cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ajax Add to Cart | |
jQuery(function($) { | |
if ($( "body" ).hasClass( "single-product" )) { | |
$(document).on("click", ".single_add_to_cart_button", function(e) { | |
var product_button = $(this); | |
$product_form = $( this ).closest('form.cart'); | |
var product_id = $product_form.find( 'button[name=add-to-cart]' ).val(); | |
product_button.addClass('loading'); | |
var n = { | |
action: "woocommerce_add_to_cart", | |
product_id: product_id, | |
}; | |
$.post(woocommerce_params.ajax_url, n, function(n) { | |
if (!n) return; | |
var r = window.location.toString(); | |
r = r.replace("add-to-cart", "added-to-cart"); | |
if (n.error && n.product_url) { | |
window.location = n.product_url; | |
return | |
} | |
if (woocommerce_params.cart_redirect_after_add == "yes") { | |
window.location = woocommerce_params.cart_url; | |
return | |
} | |
$('#header-right').addClass('cart-open'); | |
setTimeout(function(){ | |
$('#header-right').removeClass('cart-open'); | |
}, 5000); | |
//$cartURL = $('.add_to_cart_button').data("cart-url"); | |
product_button.removeClass("loading"); | |
fragments = n.fragments; | |
cart_hash = n.cart_hash; | |
if (fragments) { | |
jQuery.each(fragments, function(key, value) { | |
jQuery(key).replaceWith(value); | |
}); | |
} | |
$("body").trigger("added_to_cart", [fragments, cart_hash]) | |
//Now we set the mobile only cart quantity | |
// var cart_quantity = $('.festi-cart-quantity').text(); | |
// var mobile_cart_quantity = $('.mobile-only-cart-wrap a'); | |
// $(mobile_cart_quantity).text(cart_quantity); | |
}); | |
e.preventDefault(); | |
}); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment