Skip to content

Instantly share code, notes, and snippets.

@DanielSantoro
Last active November 22, 2022 13:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save DanielSantoro/948c5000850c4695f30db8542f8bc966 to your computer and use it in GitHub Desktop.
Save DanielSantoro/948c5000850c4695f30db8542f8bc966 to your computer and use it in GitHub Desktop.
AJAX Update Cart thats added to Template File Manually (
// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php).
// Used in conjunction with https://gist.github.com/DanielSantoro/1d0dc206e242239624eb71b2636ab148
// Compatible with WooCommerce 3.0+. Thanks to Alex for assisting with an update!
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
}
@dh-des
Copy link

dh-des commented Oct 11, 2017

Hi,

thanks for sharing this, works great.
But if i want to remove a product or change quantity in the cart via ajax its not updating. Only if i add products to the cart.
Do you have any ideas why?

@gregBerthelot
Copy link

gregBerthelot commented Nov 16, 2017

Hi @DanielSantoro

Thanks for both snippets. Works fine. Just a small issue: when cart as no product I get a plural 0 items. DO you get the same issue on your end? We should have 0 item (singular)

@Alexio454
Copy link

Alexio454 commented Dec 12, 2017

Hi,

The $woocommerce->cart->get_cart_url() produces this error:

The WC_Cart::get_cart_url function is deprecated since version 2.5. Replace with wc_get_cart_url.

I replaced it and it works fine now, without errors. Maybe you need to update your code?

function woocommerce_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	
	ob_start();
	
	?>
	<a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
	<?php
	
	$fragments['a.cart-customlocation'] = ob_get_clean();
	
	return $fragments;
	
}

@DanielSantoro
Copy link
Author

Thanks for that, alexio! Updating the snippet now. 👍

@eduard-ungureanu
Copy link

Hi, the function alone will not do anything, in my case, I also had to add the proper filter, so that function will be used:

add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment', 10, 1 );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	ob_start(); ?>
	<a class="cart-customlocation et-cart-info" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><span><?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(); ?><span></a>
	<?php $fragments['a.cart-customlocation'] = ob_get_clean();
	return $fragments;
}

@ssuhas855
Copy link

Thanks alexio, your code worked for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment