Skip to content

Instantly share code, notes, and snippets.

@alexanderdejong
Created January 20, 2018 13:45
Show Gist options
  • Save alexanderdejong/e8245b6ae968b0f86fd32dd2a0e475b8 to your computer and use it in GitHub Desktop.
Save alexanderdejong/e8245b6ae968b0f86fd32dd2a0e475b8 to your computer and use it in GitHub Desktop.
AJAX Cart
function ajax_add_to_cart(e) {
if (jQuery(this).hasClass('disabled')) {
// do nothibng
} else {
e.preventDefault();
e.stopPropagation();
jQuery('#loading-image').show();
var product_id = (jQuery(this).next('input[name="add-to-cart"]').val());
//var variation_id = (jQuery(this).next('input[name="variation_id"]').val());
var variation_id = (jQuery('.variation_id').val());
var quantity = jQuery(this).closest("form.cart").find(".quantity input[name='quantity']").val();
//var quantity = jQuery('.summary .quantity input').val();
if (quantity == null) {
var quantity = 1;
}
if (variation_id != '') {
jQuery.ajax({
url: ajax_loadcart.ajaxurl,
type:'POST',
data:'action=wc_add_cart_menu_AJAX&product_id=' + product_id + '&variation_id=' + variation_id + '&quantity=' + quantity,
success:function(data) {
jQuery('#menu-cart-menu #ajax-subtotal, p#ajax-subtotal').html(data.cart_total);
jQuery('#ajax-itemcount').html(data.cart_items_text);
jQuery.each(data.cart, function (index, data) {
jQuery('.menu-cart-contents .woocommerce-mini-cart').html('<tr class="woocommerce-mini-cart-item mini_cart_item'+data.product_stock+'">'
+'<td class="product-remove">'
+'<a href="" class="remove" aria-label="Remove this item" '
+'data-product_id="'+data.product_id+'" '
+'data-product_sku="'+data.product_sku+'" '
+'data-cart_item_key="'+data.cart_item_key+'">x</a></td>'
+'<td class="product-image">'+data.product_thumbnail+'</td>'
+'<td class="product-name"><a href="'+data.product_permalink+'">'+data.product_name+'</a></td>'
+'<td class="product-quantity" data-title="Quantità"><div class="quantity">'
+'<input type="number" class="input-text qty text" id="quantity" step="1" min="1" max="" '
+'name="cart['+data.cart_item_key+'][qty]" value="'+data.cart_item_quantity+'" title="Qtà" size="" pattern="[0-9]*" inputmode="numeric">'
+'</div></td>'
+'<td class="product-price">'+data.product_price+'</div></td>'
);
jQuery('#loading-image').hide();
jQuery('.cart-dropdown-toggle').dropdown('toggle');
jQuery('#mobile-cart').addClass('clicked').toggleClass('hidden');
jQuery('#overlay').addClass('clicked');
jQuery('#wc-ajax-add-to-cart-notices').html(data.notices);
});
}
});
} else {
jQuery.ajax({
url: ajax_loadcart.ajaxurl,
type:'POST',
data:'action=wc_add_cart_menu_AJAX&product_id=' + product_id + '&quantity=' + quantity,
success:function(results) {
jQuery('.menu-cart-contents').html(results);
}
}).always(function () {
jQuery.ajax({
url: ajax_loadcart.ajaxurl,
type: "POST",
data:'action=wc_total_cart_AJAX',
dataType: 'json',
cache: false,
success:function(data) {
jQuery('#menu-cart-menu #ajax-subtotal, p#ajax-subtotal').html(data.cart_total);
jQuery('#ajax-itemcount').html(data.cart_items);
}
});
}).always(function () {
jQuery.ajax({
url: ajax_loadcart.ajaxurl,
type:'POST',
data:'action=wc_single_product_notice_AJAX',
success:function(results) {
jQuery('#wc-ajax-add-to-cart-notices').html(results);
/*console.log(results);*/
jQuery('#loading-image').hide();
jQuery('#mobile-cart').addClass('clicked').toggleClass('hidden');
jQuery('#overlay').addClass('clicked');
jQuery('.cart-dropdown-toggle').dropdown('toggle');
}
});
});
}
}
}
function wc_add_cart_menu_AJAX()
{
if (defined('DOING_AJAX') && DOING_AJAX){
global $woocommerce;
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];
$quantity = $_POST['quantity'];
if ($variation_id) {
WC()->cart->add_to_cart($product_id, $quantity, $variation_id);
} else {
WC()->cart->add_to_cart($product_id, $quantity);
}
//$cart_items = WC()->cart->get_cart();
$cart = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = $cart_item['data'];
$product_id = $cart_item['product_id'];
// if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 ) {
$product_name = $_product->get_name();
$product_stock = $_product->get_stock_status();
$thumbnail = $_product->get_image();
$product_price = WC()->cart->get_product_price( $_product );
$product_permalink = $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '';
$product_remove = WC()->cart->get_remove_url( $cart_item_key );
$product_sku = $_product->get_sku();
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
}
$product_stock_max = $_product->get_max_purchase_quantity();
$cart_item_quantity = $cart_item['quantity'];
$cart[] = array(
'product_name' => $product_name,
'product_thumbnail' => $thumbnail,
'product_id' => $product_id,
'product_stock' => $product_stock,
'product_price' => $product_price,
'product_permalink' => $product_permalink,
'product_quantity' => $product_quantity,
'product_stock_max' => $product_stock_max,
'product_remove' => $product_remove,
'product_sku' => $product_sku,
'cart_item_key' => $cart_item_key,
'cart_item_quantity' => $cart_item_quantity,
'added_to_cart' => $quantity
);
// }
}
$notices = wc_print_notices();
$items = WC()->cart->get_cart_contents_count();
$total = WC()->cart->get_cart_total();
$cart_items_text = sprintf(_n('%d item', '%d items', $items, 'aranciogomme'), $items);
wp_send_json(
array(
"cart_count" => $items,
"cart_items_text" => $cart_items_text,
"cart_total" => $total,
"cart" => $cart,
"notices" => $notices
)
);
wp_die();
}
}
add_action('wp_ajax_wc_add_cart_menu_AJAX', 'wc_add_cart_menu_AJAX');
add_action('wp_ajax_nopriv_wc_add_cart_menu_AJAX', 'wc_add_cart_menu_AJAX');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment