Update Cart When Change quaty ajax update cart

PHOTO EMBED

Sat Aug 16 2025 02:21:12 GMT+0000 (Coordinated Universal Time)

Saved by @vanthien

(function($){
	$('body').off("click", ".qib-button").on('click','.woocommerce-mini-cart-item-info-price .qib-button',function(e){
		e.preventDefault();
		var $this	= $(this),
			$qty	= $this.parent().find('.qty'),
			$qty_val = $qty.val(),
			currentVal	= parseFloat($qty.val()),
			max			= parseFloat($qty.attr('max')),
			min			= parseFloat($qty.attr('min')),
			step		= $qty.attr('step');
		
		$(this).addClass('loading');
		// Format values
		if (!currentVal || currentVal === '' || currentVal === 'NaN') currentVal = 0;
		if (max === '' || max === 'NaN') max = '';
		if (min === '' || min === 'NaN') min = 0;
		if (step === 'any' || step === '' || step === undefined || parseFloat(step) === 'NaN') step = 1;
		
		// Change the value
		if ($this.hasClass('plus')) {
			if (max && (max == currentVal || currentVal > max)) {
				$qty_val = max;
				
			} else {
				$qty_val = currentVal + parseFloat(step);
				
				//clickThrottle = setTimeout(function() { self.quantityInputsTriggerEvents($qty); }, clickThrottleTimeout);
			}
		} else {
			if (min && (min == currentVal || currentVal < min)) {
				$qty_val = min;
				
			} else if (currentVal > 0) {
				$qty_val = currentVal - parseFloat(step);
				
				//clickThrottle = setTimeout(function() { self.quantityInputsTriggerEvents($qty); }, clickThrottleTimeout);
			}
		}
		$qty.val($qty_val);
		//console.log($qty_val);
		callChangeQTYAddToCart($qty,$qty_val);
	});
	$('body').on('added_to_cart',function(e,data) {
		$(document.body).trigger('wc_fragment_refresh').trigger('updated_cart_totals');
	});
	$(document).ajaxComplete(function () {
		//alert(1);
		/*if (cartOpen) {
		  setTimeout(function () {
			openCart();
		  }, 100);
		}
		*/
		
	  });
	$(document).on('removed_from_cart', function(event, cart_item_key) {
        console.log("Item removed with key: " + cart_item_key);
        preventRedirect = true;
		//alert(11);
		return false;
		jQuery('body').addClass('is-visible');
    });
	jQuery(document.body).on('removed_from_cart updated_cart_totals',
		function() {
			
		  	jQuery(document.body).trigger('update_checkout');
		}
	);
	jQuery('body').on('change','.woocommerce-mini-cart-item-info-price .qty',function(e){
		e.preventDefault();
		var $this	= $(this),
			$qty_val = $this.val();
		callChangeQTYAddToCart($this,$qty_val);
	})
	function callChangeQTYAddToCart($qty,$qty_val){
		var self = this;
		if (self.cartPanelAjax) {
			self.cartPanelAjax.abort();
		}
		var $cartForm = $('#nm-cart-panel-form'), 
                $cartFormNonce = $cartForm.find('#_wpnonce'),
                data = {};
		if ( ! $cartFormNonce.length ) {
			return;
		}
		data['nm_cart_panel_update'] = '1';
		data['update_cart'] = '1';
		data[$qty.attr('name')] = $qty_val;
		data['_wpnonce'] = $cartFormNonce.val();
        
		// Make call to actual form post URL.
		self.cartPanelAjax = $.ajax({
			type:     'POST',
			url:      $cartForm.attr('action'),
			data:     data,
			dataType: 'html',
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				console.log('NM: AJAX error - widgetPanelCartUpdate() - ' + errorThrown);
			},
			success:  function(response) {
				$(document.body).trigger('wc_fragment_refresh').trigger('updated_cart_totals');
				if($('body').hasClass('woocommerce-checkout'))
					jQuery(document.body).trigger('update_checkout');
			},
			complete: function() {
				self.cartPanelAjax = null; // Reset Ajax state
			}
		});
	}
})(jQuery)
jQuery(document.body).on('wc_fragment_refresh', function() {
	
    // Send AJAX to get content of minicart
    jQuery.ajax({
        url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'get_refreshed_fragments'),
        type: 'POST',
        success: function(data) {
            if (data && data.fragments) {
                // Update element
                //debugger;
                jQuery.each(data.fragments, function(key, value) {
                    jQuery(key).replaceWith(value);
                });
             
                console.log('Minicart has been refreshed');

            }
        }
    });
	
});
content_copyCOPY