B-219 Sec-55 Noida, India
+918010221733

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.

<?php

// 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 ) {
ob_start();
?>
<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>
<?php

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

return $fragments;
}
?>
(Visited 2,679 times, 1 visits today)

Leave a reply


Positioning Roulette
Positioning Roulette
UmbiliCAD Software
UmbiliCAD Software