Learn how to add custom fields to WooCommerce products easily. Follow our step-by-step guide to customize product data on your WooCommerce store.
How to Refresh or Update Your WooCommerce Mini Cart with AJAX

Table of Contents
- Introduction
- What is the Minicart?
- Why Use AJAX for the Minicart?
- Understanding the add_to_cart_fragments Hook
- How It Works
- Implementation Steps
- Key Features of Your Implementation
- Next Steps
- Conclusion
- FAQ
Enhancing Your WooCommerce Site: Mini Cart in Header with AJAX
WooCommerce is a powerful eCommerce platform for WordPress, but ensuring a seamless shopping experience requires some tweaks. One common improvement is updating the minicart dynamically with AJAX, which provides a smoother interaction for your customers. This guide will walk through how to implement this feature.
What is the Minicart?
The minicart in WooCommerce is a small, dropdown cart summary that appears when users add items to their cart. It’s a convenient way for customers to see their selected products without navigating away from their current page.

Why Use AJAX for the Minicart?
Using AJAX to refresh the minicart has several benefits:
- Better User Experience: Customers can see their cart updates in real-time without reloading the page, making the WooCommerce mini cart not updating issue a thing of the past.
- Faster Interactions: Reduces the time it takes to update the cart, keeping users engaged.
- Less Server Load: Only the necessary data is sent to the server, improving performance.
Understanding the add_to_cart_fragments
Hook
The add_to_cart_fragments
hook in WooCommerce allows you to update specific parts of the page when items are added to the cart, without refreshing the entire page. We’ll use this hook to update mini cart via AJAX in WooCommerce, effectively enabling WooCommerce refresh cart fragments.
How It Works
When a user adds a product to their cart, WooCommerce triggers an AJAX call that fetches updated cart data without refreshing the entire page. The add_to_cart_fragments
filter is applied to modify specific elements on the page based on this AJAX response.
- Custom Output Generation: Using the
add_to_cart_fragments
hook, you can generate custom HTML output that represents the current state of the cart. This output can include the number of items in the cart, the total price, or even the list of items themselves. - AJAX Response: The custom output generated within the hook is returned as a part of the AJAX response. When the cart is updated, this new content replaces the existing content on the page, ensuring users see the latest information.
- Instant Updates: This makes it easy for users to see their cart changes immediately, which is important for the WooCommerce refresh cart fragments feature.
Add the following code to your theme’s functions.php
file:
// Add to functions.php
add_filter('add_to_cart_fragments', 'codebykishor_minicart_in_header');
function codebykishor_minicart_in_header($fragments) {
ob_start();
// Output the mini cart contents
?>
<a class="cart-contents" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php esc_attr_e('View your shopping cart', 'woocommerce'); ?>">
<?php echo sprintf(_n('%d item in basket', '%d items in basket', WC()->cart->get_cart_contents_count(), 'woocommerce'), WC()->cart->get_cart_contents_count()); ?>
</a>
<?php
$fragments['.mini-cart-content'] = ob_get_clean();
return $fragments;
}
This function captures the output of the mini cart and sends it back to the browser as part of the AJAX response, enabling WooCommerce refresh cart fragments.
Displaying the Mini Cart in the Header
To display the mini cart in your header, you’ll need to modify your header template (typically header.php
). Here’s how to do it:

Add Mini Cart HTML
Insert the following code where you want the mini cart to appear in your header:
<div class="mini-cart-container">
<!-- Mini Cart Content -->
<div class="mini-cart-content">
<a class="cart-contents" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php esc_attr_e('View your shopping cart', 'woocommerce'); ?>">
<?php echo sprintf(_n('%d item in basket', '%d items in basket', WC()->cart->get_cart_contents_count(), 'woocommerce'), WC()->cart->get_cart_contents_count()); ?>
</a>
</div>
</div>
This HTML structure provides a container for your mini cart, making it easy to style and position within your theme.
Display Total Price in Mini Cart
You can modify the mini cart to show the total price of items in addition to the item count.
add_filter('add_to_cart_fragments', 'update_mini_cart_with_total');
function update_mini_cart_with_total($fragments) {
ob_start();
?>
<div class="mini-cart-content">
<a class="cart-contents" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php esc_attr_e('View your shopping cart', 'woocommerce'); ?>">
<?php
$item_count = WC()->cart->get_cart_contents_count();
$total_price = WC()->cart->get_cart_total();
echo sprintf(_n('%d item', '%d items', $item_count, 'woocommerce'), $item_count) . " - " . $total_price;
?>
</a>
</div>
<?php
$fragments['.mini-cart-content'] = ob_get_clean();
return $fragments;
}
Provide Cart Summary with Links
Add links to view or proceed to checkout within the mini cart.
add_filter('add_to_cart_fragments', 'update_mini_cart_with_links');
function update_mini_cart_with_links($fragments) {
ob_start();
?>
<div class="mini-cart-content">
<a class="cart-contents" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php esc_attr_e('View your shopping cart', 'woocommerce'); ?>">
<?php echo sprintf(_n('%d item', '%d items', WC()->cart->get_cart_contents_count(), 'woocommerce'), WC()->cart->get_cart_contents_count()); ?>
</a>
<a class="checkout-button" href="<?php echo esc_url(wc_get_checkout_url()); ?>"><?php esc_html_e('Checkout', 'woocommerce'); ?></a>
</div>
<?php
$fragments['.mini-cart-content'] = ob_get_clean();
return $fragments;
}
Custom Cart Icon Badge
Change the cart icon’s badge to reflect the number of items in the cart.
add_filter('add_to_cart_fragments', 'update_cart_icon_badge');
function update_cart_icon_badge($fragments) {
ob_start();
?>
<span class="cart-icon-badge"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
<?php
$fragments['.cart-icon-badge'] = ob_get_clean();
return $fragments;
}
AJAX Magic: Updating the Mini Cart Quantity
With the code in place, your mini cart will dynamically update whenever a user adds an item to the cart, without requiring a page reload. This is made possible through WooCommerce update mini cart quantity AJAX, creating a smoother and more interactive shopping experience for your customers.
Key Features of Your Implementation:
- Instant Updates: The minicart shows changes right away, solving problems like the WooCommerce update mini cart quantity AJAX.
- Customizable Design: You can easily tweak the HTML and CSS to ensure the mini cart aligns with your theme’s design.
- User-Friendly Experience: Customers can effortlessly track their shopping progress, leading to a more enjoyable shopping journey.
Next Steps:
- Monitor Performance: After implementation, monitor the site’s performance to ensure there are no slowdowns caused by AJAX calls.
- Customize Appearance: Adjust the CSS styles to match your brand’s look and feel. Consider changing colors, fonts, and hover effects.
- Test Across Devices: Ensure the mini cart works seamlessly on both desktop and mobile devices to provide a consistent experience.
Conclusion
Integrating a mini cart into your WooCommerce header is a powerful enhancement that significantly improves the shopping experience for your customers. By leveraging AJAX, you’ve enabled real-time updates, allowing users to see their cart contents without the hassle of page reloads. This not only fosters a more interactive and engaging environment but also helps streamline the checkout process.
FAQ
What is the purpose of the mini cart in WooCommerce?
The mini cart provides customers with a quick summary of the items they’ve added to their cart. It allows users to see their cart contents without navigating away from their current page, enhancing the shopping experience.
Will the mini cart work on mobile devices?
Absolutely! Ensure that you test the mini cart’s functionality and appearance on various devices. A responsive design will allow it to display correctly on both desktops and mobile devices.
Does the mini cart update in real-time?
Yes! The minicart updates automatically whenever items are added or removed, helping with issues like the WooCommerce mini cart not updating.
Can I customize the mini cart’s appearance?
Yes! Use CSS to style it according to your branding.
Explained more in simple words…Thanks
Thanks for the feedback! I’ll work on making my explanations clearer in future posts. I appreciate your input!