WooCommerce, a popular e-commerce plugin for WordPress, offers a feature known as “fragments.” These fragments allow specific parts of a web page to be updated without requiring the entire page to be reloaded. This is particularly useful for dynamic content, such as shopping cart totals or user points.
However, a common issue faced by developers and website owners is that these fragments don’t always update in real-time, especially when changes are made from the admin interface. Let’s delve into this issue and explore a solution based on a Stack Overflow post.
What are WooCommerce Fragments?
WooCommerce Fragments are a part of the AJAX Layer in WooCommerce. In simple terms, AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it’s possible to update parts of a web page without reloading the whole page.
Fragments in WooCommerce are essentially pieces of content that can be updated independently of the rest of the page. They play a crucial role in enhancing the user experience, especially in e-commerce settings where real-time updates (like cart contents or prices) are vital.
How are WooCommerce Fragments used?
- Cart Updates: One of the most common uses of fragments in WooCommerce is to update the cart contents and totals without reloading the page. When a user adds or removes a product from the cart, only the cart fragment is updated, providing a seamless shopping experience.
- Notifications: Fragments can be used to display real-time notifications to users. For instance, when a user adds a product to their wishlist or compares products, a notification can pop up without any page reload.
- Dynamic Pricing: If you have dynamic pricing set up, where prices change based on certain conditions (like user role or quantity), fragments can be used to display the updated price instantly.
- User-specific Content: Fragments can be used to show user-specific content, such as loyalty points, personalized recommendations, or user-specific offers, without having to reload the entire page.

The Problem
A developer created a plugin that uses WooCommerce fragments to display the number of points a user has. When a user spends some of their points (by adding a product to the cart), the fragment updates correctly. However, if an admin adds points to a user via the admin interface, the fragment doesn’t update until the user adds a new product to the cart.
Here’s a simplified version of the code they used:
function create_points_widget_content() {
$usersPoints = esc_attr( get_user_meta( get_current_user_id(), USERS_POINTS, true ));
if (!isset($userPoints) || $userPoints == "") $userPoints = 0;
$usedPoints = get_current_points_in_cart('', 0);
?>
<div class="points-widget-v2">
<div><?php echo __("Your points", "plugin-domain"); ?>:<span class="users-points"><?php echo $usersPoints; ?></span></div>
<div><?php echo __("Actually used", "plugin-domain"); ?>:<span class="used-points"><?php echo $usedPoints; ?></span></div>
</div>
<?php
}
The question then arises: How can we force these fragments to refresh themselves?
The Solution
One of the answers provided a simple and effective solution using JavaScript:
function refresh_fragments() {
console.log('fragments refreshed!');
$( document.body ).trigger( 'wc_fragment_refresh' );
}
refresh_fragments();
setInterval(refresh_fragments, 60000);
This code triggers a core handler in WooCommerce to refresh fragments via AJAX. The setInterval
function ensures that the fragments are refreshed every 60 seconds.
Key Takeaways
- WooCommerce Fragments: These are parts of a web page that can be updated without reloading the entire page. They’re useful for displaying dynamic content.
- Admin Interface Limitation: Changes made from the admin interface might not immediately reflect on the frontend due to fragment caching.
- JavaScript Solution: Using JavaScript, it’s possible to force the fragments to refresh at regular intervals, ensuring that the displayed content is always up-to-date.
For business owners or new developers, it’s essential to understand that while WooCommerce offers powerful features, there might be occasional hiccups. However, with the right knowledge and resources, these challenges can be easily overcome.