Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 19, 2012

not working

@raisonon

This comment has been minimized.

Copy link

@raisonon raisonon commented Nov 29, 2012

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

@seafarer

This comment has been minimized.

Copy link

@seafarer seafarer commented Dec 14, 2012

Doesn't work for me either.

@Preeminent

This comment has been minimized.

Copy link

@Preeminent Preeminent commented Jan 6, 2013

Works like a charm!

@drifterz28

This comment has been minimized.

Copy link

@drifterz28 drifterz28 commented Jan 16, 2013

I could not get this to work.

@jacquesletesson

This comment has been minimized.

Copy link

@jacquesletesson jacquesletesson commented Jan 24, 2013

It totally works like a charm!

I was wondering if it is possible to display the woocommerce message/error after adding content to the cart with AJAX ?

@gothical

This comment has been minimized.

Copy link

@gothical gothical commented Mar 11, 2013

Brilliant! Works fluidly!

@vtrixitsolutions

This comment has been minimized.

Copy link

@vtrixitsolutions vtrixitsolutions commented Jun 5, 2013

Works great! Thanks

@chita1sahoo

This comment has been minimized.

Copy link

@chita1sahoo chita1sahoo commented Jun 7, 2013

Not working in my website.

@josesayago

This comment has been minimized.

Copy link

@josesayago josesayago commented Jul 12, 2013

What WooCommerce Documentation (http://docs.woothemes.com/document/show-cart-contents-total/) takes for granted is you'll copy everything as is. However, I think it's important to note there is a connexion between links classes:

...<a class="cart-contents" href="... // Class cart-contents (this link goes in your template file)

and

$fragments['a.cart-contents'] = ob_get_clean(); // Class cart-contents (this goes in your functions.php)

If you change link class then you must change fragments class, otherwise the ajax counter won't work. Keep in mind if you change the content from an 'a' element to something else (a div for example), then remember to change the fragments' element as well:

...<div class="cart-contents"...

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

I was wondering why it wasn't working until I figured out I had changed link classes in my template file but forgot to do it in my functions.php file.

Cheers.

@contempoinc

This comment has been minimized.

Copy link

@contempoinc contempoinc commented Sep 20, 2013

Doesn't work for me either, copy/pasted the exact function. Received this error:

Warning: Missing argument 1 for woocommerce_header_add_to_cart_fragment(), called in C:\wamp\www\wordpress\wp-content\themes\serenity\header.php on line 112 and defined in C:\wamp\www\wordpress\wp-content\themes\serenity\admin\ct-woocommerce.php on line 27

@winddweb

This comment has been minimized.

Copy link

@winddweb winddweb commented Apr 12, 2014

Just like @laelitenetwork said, please remember

If you change link class then you must change fragments class, otherwise the ajax counter won't work.

For example, I used a span to show just the count number,

<span class="cart-count">cart->cart_contents_count; ?></span>

I will add the following code to my functions.php

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');

function woocommerce_header_add_to_cart_fragment( $fragments ) {
  global $woocommerce;

  ob_start();

  ?>
  <span class="cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
  <?php

  $fragments['span.cart-count'] = ob_get_clean();

  return $fragments;

}
@maheshwaghmare

This comment has been minimized.

Copy link

@maheshwaghmare maheshwaghmare commented Jun 10, 2014

Awesome Works...!

@dingo-d

This comment has been minimized.

Copy link

@dingo-d dingo-d commented Sep 17, 2014

Tried adding this, but it only updated the cart number, the price changed only after I refreshed the page (number changes instantly). Is there a way to make price total prints out automatically as well?

@webworldzero

This comment has been minimized.

Copy link

@webworldzero webworldzero commented Oct 14, 2014

It doesn't work! i'm getting blank screen when i add the code in functions.php

@frytaz

This comment has been minimized.

Copy link

@frytaz frytaz commented Oct 17, 2014

Thanks, works just fine :)

@iMazed

This comment has been minimized.

Copy link

@iMazed iMazed commented Jan 30, 2015

Worked for me, thanks!

@sebszocinski

This comment has been minimized.

Copy link

@sebszocinski sebszocinski commented Sep 24, 2015

This doesn't appear to work anymore now that woocommerce_add_to_cart_fragments is somewhat depreciated but still in the core

@neonics

This comment has been minimized.

Copy link

@neonics neonics commented Sep 24, 2015

Works fine, HOWEVER take note during development/testing, that:

  • the fragments are cached, so if you change the layout, empty the basket!
  • the fragments are also applied on/after page load, not only when an item is added to the cart.
@jandosul

This comment has been minimized.

Copy link

@jandosul jandosul commented Dec 8, 2015

@neonics, Thanks a lot for note about caching fragments

@alewolf

This comment has been minimized.

Copy link

@alewolf alewolf commented Dec 9, 2015

Hmm. Doesn't work for me. How can I debug that?

@ActionScripted

This comment has been minimized.

Copy link

@ActionScripted ActionScripted commented Jan 5, 2016

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

@enkot

This comment has been minimized.

Copy link

@enkot enkot commented Feb 16, 2016

Works. Thx.

@suifengtec

This comment has been minimized.

Copy link

@suifengtec suifengtec commented Feb 28, 2016

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

This comment has been minimized.

Copy link

@immyryb immyryb commented Apr 26, 2016

@nanangrukmana

This comment has been minimized.

Copy link

@nanangrukmana nanangrukmana commented Jun 2, 2016

works. thank you.

@purshottammudgal

This comment has been minimized.

Copy link

@purshottammudgal purshottammudgal commented Jun 3, 2016

Awesome Works...! thank you.

@photon43

This comment has been minimized.

Copy link

@photon43 photon43 commented Jun 30, 2016

Works as expected. Thank you!

@LouVerdun

This comment has been minimized.

Copy link

@LouVerdun LouVerdun commented Aug 14, 2016

Thank you very much, works perfectly !

@rajanvijayan

This comment has been minimized.

Copy link

@rajanvijayan rajanvijayan commented Oct 28, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@eblosch eblosch commented Jun 7, 2017

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

@angelfilix

This comment has been minimized.

Copy link

@angelfilix angelfilix commented Jun 13, 2017

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

@tomhermans

This comment has been minimized.

Copy link

@tomhermans tomhermans commented Oct 3, 2017

thanks. just what i was looking for

@Jordanairwave

This comment has been minimized.

Copy link

@Jordanairwave Jordanairwave commented Nov 9, 2017

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

@Alekuoshu

This comment has been minimized.

Copy link

@Alekuoshu Alekuoshu commented Mar 27, 2018

Its work, thanks @winddweb

@Arifi

This comment has been minimized.

Copy link

@Arifi Arifi commented Apr 29, 2018

Works as charm !!

@lukoie

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@akmahaz11 akmahaz11 commented Jul 4, 2019

@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

This comment has been minimized.

Copy link

@rajanvijayan rajanvijayan commented Jul 8, 2019

@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

This comment has been minimized.

Copy link

@akmahaz11 akmahaz11 commented Jul 8, 2019

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

@rajanvijayan

This comment has been minimized.

Copy link

@rajanvijayan rajanvijayan commented Jul 8, 2019

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

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

@akmahaz11

This comment has been minimized.

Copy link

@akmahaz11 akmahaz11 commented Jul 8, 2019

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

@akmahaz11

This comment has been minimized.

Copy link

@akmahaz11 akmahaz11 commented Jul 8, 2019

@rajanit2000, I am using Flatsome woocommerce theme

@rajanvijayan

This comment has been minimized.

Copy link

@rajanvijayan rajanvijayan commented Jul 8, 2019

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

@akmahaz11

This comment has been minimized.

Copy link

@akmahaz11 akmahaz11 commented Jul 8, 2019

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

@hovek

This comment has been minimized.

Copy link

@hovek 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.