<?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; | |
} |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
Doesn't work for me either. |
This comment has been minimized.
This comment has been minimized.
Works like a charm! |
This comment has been minimized.
This comment has been minimized.
I could not get this to work. |
This comment has been minimized.
This comment has been minimized.
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 ? |
This comment has been minimized.
This comment has been minimized.
Brilliant! Works fluidly! |
This comment has been minimized.
This comment has been minimized.
Works great! Thanks |
This comment has been minimized.
This comment has been minimized.
Not working in my website. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Just like @laelitenetwork said, please remember
For example, I used a span to show just the count number,
I will add the following code to my functions.php
|
This comment has been minimized.
This comment has been minimized.
Awesome Works...! |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
It doesn't work! i'm getting blank screen when i add the code in functions.php |
This comment has been minimized.
This comment has been minimized.
Thanks, works just fine :) |
This comment has been minimized.
This comment has been minimized.
Worked for me, thanks! |
This comment has been minimized.
This comment has been minimized.
This doesn't appear to work anymore now that woocommerce_add_to_cart_fragments is somewhat depreciated but still in the core |
This comment has been minimized.
This comment has been minimized.
Works fine, HOWEVER take note during development/testing, that:
|
This comment has been minimized.
This comment has been minimized.
@neonics, Thanks a lot for note about caching fragments |
This comment has been minimized.
This comment has been minimized.
Hmm. Doesn't work for me. How can I debug that? |
This comment has been minimized.
This comment has been minimized.
@neonics, thanks (again) for the note about caching! |
This comment has been minimized.
This comment has been minimized.
Works. Thx. |
This comment has been minimized.
This comment has been minimized.
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');
} |
This comment has been minimized.
This comment has been minimized.
use this plugin... https://wordpress.org/plugins/woocommerce-ajax-cart/ |
This comment has been minimized.
This comment has been minimized.
works. thank you. |
This comment has been minimized.
This comment has been minimized.
Awesome Works...! thank you. |
This comment has been minimized.
This comment has been minimized.
Works as expected. Thank you! |
This comment has been minimized.
This comment has been minimized.
Thank you very much, works perfectly ! |
This comment has been minimized.
This comment has been minimized.
Use this code on functions.php
Use this code on theme file (may be header.php)
|
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
Thanks, @winddweb, solved the problem I was having. |
This comment has been minimized.
This comment has been minimized.
Hi If using the WooCommerce Dynamic Pricing extension the discount is not taken into account. Ideas how to display cart total - |
This comment has been minimized.
This comment has been minimized.
thanks. just what i was looking for |
This comment has been minimized.
This comment has been minimized.
Thank you dude, Code work straight away and was just want I was looking for. |
This comment has been minimized.
This comment has been minimized.
Its work, thanks @winddweb |
This comment has been minimized.
This comment has been minimized.
Works as charm !! |
This comment has been minimized.
This comment has been minimized.
Guys, this code WONT work if you're translating something not in the main WP language file.
note this addition by me: so now you can use your POedit(or bare hands), and translate the string "%d item"
|
This comment has been minimized.
This comment has been minimized.
@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 |
This comment has been minimized.
This comment has been minimized.
Do you have any console error when try to update the cart? Can you please share your URL for better understanding |
This comment has been minimized.
This comment has been minimized.
@rajanit2000, https://awalbathonline.com/home-eng.. plse check |
This comment has been minimized.
This comment has been minimized.
@akmahaz11 But I can't find https://drive.google.com/file/d/1rLfQtMh8Am2qzZr8J7Kcv7YUq0abyTdm/view?usp=sharing |
This comment has been minimized.
This comment has been minimized.
@rajanit2000, This is the issue, the header cart is not getting updated... how can i resolve this? |
This comment has been minimized.
This comment has been minimized.
@rajanit2000, I am using Flatsome woocommerce theme |
This comment has been minimized.
This comment has been minimized.
Can you please share your header.php file via email? |
This comment has been minimized.
This comment has been minimized.
@rajanit2000, Ihave sent email for you ... please check your mail inbox |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
not working