Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Forked from jameskoster/header.php
Last active October 31, 2023 04:24
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save mikejolley/2044109 to your computer and use it in GitHub Desktop.
Save mikejolley/2044109 to your computer and use it in GitHub Desktop.
WooCommerce - Update number of items in cart and total after Ajax
<?php
// Ensure cart contents update when products are added to the cart via AJAX (place the following 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;
}
@ActionScripted
Copy link

@neonics, thanks (again) for the note about caching!

@enkot
Copy link

enkot commented Feb 16, 2016

Works. Thx.

@suifengtec
Copy link

global $woocommerce;
if( version_compare( $woocommerce->version, '2.3', '<' ) ){
    // WC 2.3 -
    add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
} else {
    // WC 2.3 +
    add_filter('woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
}

@immyryb
Copy link

immyryb commented Apr 26, 2016

@nanangrukmana
Copy link

works. thank you.

@purshottammudgal
Copy link

Awesome Works...! thank you.

@photon43
Copy link

Works as expected. Thank you!

@LouVerdun
Copy link

Thank you very much, works perfectly !

@rajanvijayan
Copy link

Use this code on 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;
}

Use this code on theme file (may be header.php)

<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>

@huykon
Copy link

huykon commented May 31, 2017

it's not working with me. I have tried click add to cart Ajax and then see the number of mini cart is not updated. How to fix it?

@eblosch
Copy link

eblosch commented Jun 7, 2017

Thanks, @winddweb, solved the problem I was having.

@angelfilix
Copy link

Hi If using the WooCommerce Dynamic Pricing extension the discount is not taken into account. Ideas how to display cart total -

@tomhermans
Copy link

thanks. just what i was looking for

@Jordanairwave
Copy link

Thank you dude, Code work straight away and was just want I was looking for.

@Alekuoshu
Copy link

Its work, thanks @winddweb

@Arifi
Copy link

Arifi commented Apr 29, 2018

Works as charm !!

@lukoie
Copy link

lukoie commented Nov 13, 2018

Guys, this code WONT work if you're translating something not in the main WP language file.
Because the code lack the textdomain. Instead it shoul look like this:


<?php
// Ensure cart contents update when products are added to the cart via AJAX (place the following 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(), 'woocommerce' ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a> 
	<?php
	
	$fragments['a.cart-contents'] = ob_get_clean();
	
	return $fragments;
}

note this addition by me:
_n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'woocommerce' ), WC()->cart->get_cart_contents_count()

so now you can use your POedit(or bare hands), and translate the string "%d item"
In the .po file it should look something like this:

msgid "%d item"
msgid_plural "%d items"
msgstr[0] "%d блюдо"
msgstr[1] "%d блюда"
msgstr[2] "%d блюд"

@akmahaz11
Copy link

@rajanit2000 i did as per code.. its was working but it another minicart not in the same mini cart as of theme. while theme minicart is still not updating... can you help me plse

@rajanvijayan
Copy link

@rajanit2000 i did as per code.. its was working but it another minicart not in the same mini cart as of theme. while theme minicart is still not updating... can you help me plse

Do you have any console error when try to update the cart? Can you please share your URL for better understanding

@akmahaz11
Copy link

@rajanit2000, https://awalbathonline.com/home-eng.. plse check

@rajanvijayan
Copy link

@akmahaz11 But I can't find cart-contents this class, Refer my screenshot

https://drive.google.com/file/d/1rLfQtMh8Am2qzZr8J7Kcv7YUq0abyTdm/view?usp=sharing

@akmahaz11
Copy link

@rajanit2000, This is the issue, the header cart is not getting updated... how can i resolve this?

@akmahaz11
Copy link

@rajanit2000, I am using Flatsome woocommerce theme

@rajanvijayan
Copy link

Can you please share your header.php file via email?

@akmahaz11
Copy link

@rajanit2000, Ihave sent email for you ... please check your mail inbox

@hovek
Copy link

hovek commented Feb 3, 2020

I've added throw new Exception('In function.'); to test if the function woocommerce_header_add_to_cart_fragment is being called and it's never called. Why?

@ruman
Copy link

ruman commented Aug 24, 2021

To make this hook work perfectly just consider 2 things.

  1. set unique classname for the navbar cart icon like <div class="nav-cart"><i class="fa fa-busket"></i><span class="nav-cart-total"> <?php $count;?></span>/div>
  2. in that filter have to edit this:
$cart_count = WC()->cart->cart_contents_count;
<span class="nav-cart-total"><?php echo $cart_count; ?></span>
	
	<?php $fragments['span.nav-cart-total'] = ob_get_clean(); ?>

Will work delightfully!!!

@rvpatel
Copy link

rvpatel commented Jan 21, 2022

Why not work on cart page and how to fix easy way?

@rvpatel
Copy link

rvpatel commented Jan 21, 2022

Update count when cart page with ajax update.

jQuery(document.body).on('removed_from_cart updated_cart_totals', function () {
		$(document.body).trigger('wc_fragment_refresh');
});

@rafiul
Copy link

rafiul commented Apr 15, 2022

@rajanvijayan Working perfectly. Thank you.

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