Skip to content

Instantly share code, notes, and snippets.

@CrispDev
Last active April 20, 2019 19:27
Show Gist options
  • Save CrispDev/ad0ae0edb3bad5cc4e71195437bb42b8 to your computer and use it in GitHub Desktop.
Save CrispDev/ad0ae0edb3bad5cc4e71195437bb42b8 to your computer and use it in GitHub Desktop.
Woocommerce with Autoship Powered by QPilot Ajax Add To Cart Button
jQuery( function($) {
/* ------------------------------------------------------------------------------------ */
/* Document Ready */
/* ------------------------------------------------------------------------------------ */
$(document).ready( function() {
// Attach the Ajax class to the add-to-cart button
$(".single_add_to_cart_button").addClass("ajax_add_to_cart");
// Attach a listener for when a customer clicks on the Ajax Add to Cart
$(document).on('click', '.ajax_add_to_cart', function(e) {
e.preventDefault();
// wc_add_to_cart_params is required to continue, ensure the object exists
if (typeof woocommerce_params === 'undefined') return false;
var $thisbutton = $(this);
// Used to Store the Autoship element
var autoship_wrapper;
var variations = $(this).closest(".variations_form");
// Get the closest variation for to the button
if ( variations.length ) {
// Capture the Product Variation ID, QTY, attach the action and variation
// details.
var $variation_form = $( $(this).closest(".variations_form")[0] );
var product_id = $variation_form.find('input[name=product_id]').val();
var quantity = $variation_form.find('input[name=quantity]').val();
var variation_id = $variation_form.find('input[name=variation_id]').val();
var action = 'wp_ajax_nopriv_woocommerce_add_to_cart';
var data = {
action: action,
product_id: variation_id,
quantity: quantity,
//variation_id: variation_id
};
// Grab the Autoship wrapper for this product variation.
autoship_wrapper = $(this).parents('.single_variation_wrap').find(".autoship-schedule-options");
} else {
var product_id = $(this).val();
var quantity = $(this).parent().find(".qty").val();
var action = 'wp_ajax_woocommerce_add_to_cart';
// Grab the Autoship wrapper for this product.
autoship_wrapper = $(this).parent().find(".autoship-schedule-options");
var data = {
action: action,
product_id: product_id,
quantity: quantity
};
}
// Also Grab the Autoship details if this is an autoship variation o rproduct
if ( autoship_wrapper.length ){
// Since this is an Autoship product attach the selected autoship values to the data.
data.autoship = autoship_wrapper.find('input[name=autoship]:checked').val();
data.autoship_frequency = autoship_wrapper.find('input[name=autoship_frequency]').val();
data.autoship_frequency_type = autoship_wrapper.find('input[name=autoship_frequency_type]').val();
}
// Manually trigger the add to cart
// Trigger event.
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
// Send the product data via ajax // Ajax action.
$.post( woocommerce_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
if ( ! response ) {
return;
}
/**
* Update The cart
*/
var cart_price = $('.cart-price');
cart_price.html( response['fragments']['.cart-price'] );
var cart_icon = $('.header .cart-icon');
cart_icon.html( response['fragments']['.header .cart-icon'] );
var cart_content = $('div.widget_shopping_cart_content');
cart_content.html( response['fragments']['div.widget_shopping_cart_content'] );
// Custom to Theme used.
$('.header-cart-link').closest('.cart-item').addClass('current-dropdown');
return false;
});
return false;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment