Ecommerce Shopify WordPress Discussion

WooCommerce – PHP Output Buffering – Why does my cart content number not change on add to cart?

I have this HTML / PHP markup that I want to change the number of items in the cart when a user adds an item to the cart. Right now, it only increases the number of items in the cart on page refresh - not dynamically like I want it to. $cart = WC()->cart; if ( $cart ) { $cart_contents_count = $cart->get_cart_contents_count(); $cart_contents_total = $cart->total; } $checkout_url = get_permalink( get_option( 'woocommerce_checkout_page_id' ) ); ?> <div class="ecommerce-nav"> <!-- Cart Link --> <a id="open-cart" class="sa-link" href="/cart/" data-open-wfc="true"> <span title="<?php echo $cart_contents_count ?> items in your cart" aria-label="<?php echo $cart_contents_count ?> items in your cart" class="shopping-cart-icon"> <?php //Icon require_once( get_template_directory() . '/assets/images/header-icons/Cart.svg' ); //Number of items if ( $cart_contents_count > 0 ) { echo '<span class="shopping-cart-contents-count">' . $cart_contents_count . '</span>'; } ?> </span> <span class="cart-contents-total sa-link__description"> <?php echo ( $cart_contents_total > 0 ) ? '<sup>$</sup>' . $cart_contents_total : ""; ?> </span> </a> // More items and code here Here's my function that executes on the Woocommerce Cart Fragments filter hook: function woocommerce_header_add_to_cart_fragment( $fragments ) { $cart = WC()->cart; if ( $cart ) { $cart_contents_count = $cart->get_cart_contents_count(); $cart_contents_total = $cart->total; } ob_start(); ?> <a id="open-cart" class="sa-link" href="/cart/" data-open-wfc="true"> <span title="<?php echo $cart_contents_count ?> items in your cart" aria-label="<?php echo $cart_contents_count ?> items in your cart" class="shopping-cart-icon"> <?php //Icon require_once( get_template_directory() . '/assets/images/header-icons/Cart.svg' ); //Number of items if ( $cart_contents_count > 0 ) { echo '<span class="shopping-cart-contents-count">' . $cart_contents_count . '</span>'; } ?> </span> <span class="sa-link__description"> <?php echo ( $cart_contents_total > 0 ) ? '<sup>$</sup>' . $cart_contents_total : ""; ?> </span> </a> <?php $fragments['#open-cart'] = ob_get_clean(); return $fragments; } add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' ); I want this line:... echo '<span class="shopping-cart-contents-count">' . $cart_contents_count . '</span>'; ...and this line... <?php echo ( $cart_contents_total > 0 ) ? '<sup>$</sup>' . $cart_contents_total : ""; ?> ...To Update when a user clicks add to cart. What am I doing wrong? Thanks in advance.
Ah - it appears in WooCommerce 7.8, the refreshed fragments script isn't loaded unless you are using the mini cart functionality. I fixed this by adding this line of code in my wp_enqueue_scripts callback: wp_enqueue_script( 'wc-cart-fragments' ); More information on this here.

February 4, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.