Woocommerce mini cart ajax update

Here’s the quick and dirty.

add_filter('woocommerce_add_to_cart_fragments', 'your_own_add_to_cart_fragment');

function your_own_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	ob_start();
	?>
        //Update basket counter
	<div class="basket-counter"><?php echo WC()->cart->get_cart_contents_count(); ?></div>
	<?php
	$fragments['.basket-counter'] = ob_get_clean();
	return $fragments;
}

So what’s happening here?

Woocomerce will find the Fragment node and replace it with the output buffer.

That means we can update any element using the add_to_cart button.

Yes any..

Want to replace the logo with the total amount of items in the basket? No problem

function your_own_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	ob_start();
	?>
        //Replace header logo with cart contents count
	<div class="header__logo"><?php echo WC()->cart->get_cart_contents_count(); ?></div>
	<?php
        //Element to be replaced
	$fragments['.header__logo'] = ob_get_clean();
	return $fragments;
}

To review

Get Woocommerce object

global $woocommerce;	

Start output buffer

ob_start();

New content to display on add to cart

<?php echo $my_new_content; ?>
//Remember the Fragment node is replaced so you need to wrap content in html element. 

Note: $fragment Key is used to find node to be replaced. So you could do something like:

    $fragments['.basket-counter'] = ob_get_contents();
    $fragments['.header__logo > strong, .basket-counter'] = ob_get_contents();
    

Notice I used

ob_get_contents()

instead of

ob_get_clean()

That’s really all there is to it.

Subscribe to our mailing list

View previous campaigns.

15 thoughts on “Woocommerce mini cart ajax update

  • Great post! Great post! hermosa marketing and redondo beach. need help with hermosa ppc advertising structures?

  • Good post. I learn something more challenging on different blogs everyday. It’ll always be stimulating to read content material from other writers and follow a little bit one thing from their store. I’d prefer to use some with the content on my blog whether or not you don’t mind. Natually I’ll offer you a hyperlink on your web blog. Thanks for sharing.

  • Good post. I be taught something more difficult on different blogs everyday. It’ll always be stimulating to learn content material from other writers and apply a bit one thing from their store. I’d choose to make use of some with the content material on my weblog whether you don’t mind. Natually I’ll give you a hyperlink in your internet blog. Thanks for sharing.

  • Aw, this was a really nice post. In thought I would like to put in writing like this moreover – taking time and actual effort to make a very good article… however what can I say… I procrastinate alot and on no account seem to get one thing done.

  • You need to take part in a contest for among the best blogs on the web. I will advocate this web site!

  • Oh my goodness! an amazing article dude. Thank you However I am experiencing concern with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting an identical rss problem? Anybody who is aware of kindly respond. Thnkx

  • I used to be more than happy to search out this net-site.I needed to thanks on your time for this glorious read!! I positively enjoying each little bit of it and I have you bookmarked to check out new stuff you blog post.

  • There are certainly lots of particulars like that to take into consideration. That could be a nice point to carry up. I offer the ideas above as general inspiration but clearly there are questions like the one you deliver up where crucial thing shall be working in honest good faith. I don?t know if best practices have emerged around issues like that, but I’m positive that your job is clearly recognized as a fair game. Each girls and boys really feel the impression of just a second’s pleasure, for the rest of their lives.

  • An fascinating dialogue is value comment. I think that it’s best to write more on this matter, it may not be a taboo subject however typically people are not sufficient to speak on such topics. To the next. Cheers

  • Would you be enthusiastic about exchanging links?

  • Would you be involved in exchanging links?

  • It’s laborious to search out educated individuals on this matter, but you sound like you realize what you’re talking about! Thanks

  • Great post! Great post! hermosa marketing and redondo beach.

Leave a Reply