B-219 Sec-55 Noida, India

How to update cart in woocommerce using ajax

Update woocommerce cart without reloading the page via ajax.

In woocommerce if you add an item in cart then the cart needs to be updated which happens only after you reload the page.

Write this code in your theme’s functions.php to refresh the cart using ajax without reloading the page.

// Cart contents are updated when products are added to the cart via AJAX (place it in functions.php)
add_filter( ‘woocommerce_add_to_cart_fragments’, ‘woocommerce_header_add_to_cart_fragment’ );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( ‘View your shopping cart’ ); ?>"><?php echo sprintf (_n( ‘%d item’, ‘%d items’, WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> – <?php echo WC()->cart->get_cart_total(); ?></a>

$fragments[‘a.cart-contents’] = ob_get_clean();

return $fragments;

(Visited 3,387 times, 2 visits today)

Leave a reply

You must be logged in to post a comment.
Positioning Roulette
Positioning Roulette
UmbiliCAD Software
UmbiliCAD Software