Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guytzhak/a958e88ae0c5a412be995216ef5cc982 to your computer and use it in GitHub Desktop.
Save guytzhak/a958e88ae0c5a412be995216ef5cc982 to your computer and use it in GitHub Desktop.
Woocommerce update quantities in cart
$('body').on('change keyup input', '.quantity_mini input', function () {
var qtyMiniWrapper = $(this).closest('.quantity_mini'),
qtyMiniUpdateBtn = qtyMiniWrapper.find('button.update'),
defaultQty = parseFloat(qtyMiniUpdateBtn.attr('data-default_qty')),
selectedQty = parseFloat($(this).val());
if( defaultQty !== selectedQty ) {
qtyMiniUpdateBtn.fadeIn('350');
} else {
qtyMiniUpdateBtn.fadeOut('350');
}
})
$('body').on('click', '.quantity_mini button.update', function () {
var thisEl = $(this).closest('.quantity_mini').find('input.qty'),
cartItemKey = thisEl.attr('name'),
cartItemQty = thisEl.val(),
thisBtn = $(this);
jQuery.ajax({
url: mz_obj.ajax_url,
type: 'post',
data: {
action: 'mz_cart_item_change_qty',
cart_item_key: cartItemKey,
quantity: cartItemQty,
},
beforeSend: function() {
$('.cart_loader').fadeIn();
},
success: function (response) {
$('.cart_loader').fadeOut();
//$( document.body ).trigger( 'wc_fragment_refresh' );
if( response.success ) {
thisBtn.fadeOut('350').attr('data-default_qty', parseFloat(cartItemQty));
if(response.data.price != undefined ) {
thisEl.closest('.woocommerce-mini-cart-item').find('.price').html(response.data.price);
}
if(response.data.total != undefined ) {
$('.checkout.wc-forward').html(response.data.total);
$('.cart_total').html(response.data.total_price);
}
if ($('body').hasClass('woocommerce-checkout')) {
$(document.body).trigger('update_checkout');
}
}
},
error: function (errorThrown) {
console.log(errorThrown);
}
});
});
/*
* add filter to 'woocommerce_cart_item_quantity' filter.
* To add custom "update" btn per cart item
* Priority: 99
*
*/
add_filter( 'woocommerce_cart_item_quantity', 'mz_woocommerce_cart_item_quantity', 99, 3);
function mz_woocommerce_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ) {
$default_qty = $cart_item['quantity'];
$add_html = '<button type="button" data-default_qty="'.$default_qty.'" class="update">'. __('עדכן', 'cs') .'</button>';
return $product_quantity . $add_html;
}
/** AJAX PHP Action **/
add_action( 'wp_ajax_mz_cart_item_change_qty', 'mz_cart_item_change_qty' );
add_action( 'wp_ajax_nopriv_mz_cart_item_change_qty', 'mz_cart_item_change_qty' );
function mz_cart_item_change_qty() {
$key = isset($_POST['cart_item_key']) && !empty($_POST['cart_item_key']) ? sanitize_text_field($_POST['cart_item_key']) : false;
$quantity = isset($_POST['quantity']) && !empty($_POST['quantity']) ? sanitize_text_field($_POST['quantity']) : false;
if( $key && $quantity ) {
WC()->cart->set_quantity($key, $quantity);
$cart_item = WC()->cart->get_cart_item($key);
$_product = $cart_item['data'];
$data['price'] = WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] );
$data['total'] = __('לתשלום', 'cs') . wc_price(WC()->cart->get_cart_contents_total());
$data['total_price'] = wc_price(WC()->cart->get_cart_contents_total());
}
wp_send_json_success( $data );
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment