WooCommerce: Slow charge for 'wc-ajax=get_refreshed_fragments'

WooCommerce: Slow charge for 'wc-ajax=get_refreshed_fragments'

WooCommerce is the most popular plugin for creating online stores in WordPress. This plugin uses AJAX technology to provide a smooth and dynamic user experience. One of the key AJAX calls is wc-ajax=get_refreshed_fragments, designed to update shopping cart data in real time without reloading the entire page. Although essential for cart functionality, this feature can negatively impact site performance if not handled carefully.
 
How Does wc-ajax=get_refreshed_fragments Work?
When customers add products to the cart or make changes to it, WooCommerce makes a call to /?wc-ajax=get_refreshed_fragments to update the cart fragments on the page. This means that details such as the number of items and the cart total are updated instantly without needing to reload the full page.
 
Generation of Slow Loads
The problem begins when this request is executed on all pages, even on those where an updated cart display is not necessary. This can happen, for example, if the cart or its mini-cart version is included on every page through a widget. The result is an excessive number of calls to admin-ajax.php, increasing server load and slowing down the user experience.
 
What Are Requests to admin-ajax.php?
admin-ajax.php is an essential file in WordPress that manages AJAX requests. AJAX (Asynchronous JavaScript and XML) enables communication between the browser and the server, allowing parts of a page to be updated without fully reloading it. WooCommerce, like other WordPress plugins and themes, uses admin-ajax.php to execute backend operations without interrupting the frontend user experience.
 
Excessive requests to admin-ajax.php, especially on high-traffic sites or those with many dynamic elements, can overload the server. This results in slower load times and potentially a poorer user experience, as well as a negative impact on the site's SEO.
 
Practical Solutions to Optimize wc-ajax=get_refreshed_fragments
Identifying AJAX Requests with Query Monitor
 
To use Query Monitor, you must install and activate the plugin on your WooCommerce website.
 
 
Query Monitor is a plugin used for managing WordPress installations with WooCommerce. This plugin allows you to view AJAX requests on your site. After installing and activating Query Monitor, you will be able to:
 
- Identify unnecessary requests: Browse your site and observe the requests to admin-ajax.php made on each page. Query Monitor will show details about these requests, helping you identify which are essential and which could be removed.
 
- Detect overloads: Pay special attention to pages that do not require dynamic cart updates, such as informational pages, blog posts, or contact sections. If these pages are generating requests to admin-ajax.php, it is a sign that they can be optimized.
 
To test the 'wc-ajax=get_refreshed_fragments’ requests, go to your website’s shopping cart page and add some products to the cart. Then open the plugin’s management window and access the WooCommerce processes to monitor them.
 
 
Using the disable_ajax_requests Function to Disable Requests
Once unnecessary requests have been identified, you can selectively disable them using a custom function in your WordPress theme’s functions.php file. To access and edit the functions.php file of your WordPress theme, we recommend accessing the theme management section from your WordPress admin panel.
 
 
The disable_ajax_requests function can be adapted to disable requests on specific pages:
 
function disable_ajax_requests() {
    if ( ! is_cart() && ! is_checkout() && ! is_account_page() ) {
        wp_dequeue_script( 'wc-cart-fragments' );
        wp_dequeue_script( 'woocommerce' );
        wp_dequeue_script( 'wc-add-to-cart' );
        remove_action( 'wp_head', 'wc_fragment_refresh', 10);
    }
}
add_action( 'wp_enqueue_scripts', 'disable_ajax_requests', 100 );
 
Note: This code disables AJAX requests related to the cart on pages that are not essential for cart functionality, such as cart, checkout, or account pages. Make sure to test your site after implementing these changes to verify everything works as expected.
 
Additional Performance Improvements
In addition to AJAX requests, there are other strategies you can implement to further improve performance:
 
- Image Optimization: We recommend using image optimization plugins to reduce image size without losing quality.
- CSS and JavaScript Minification: Plugins such as Autoptimize can help you minimize and combine CSS and JavaScript files, reducing HTTP requests and page load time.
- Use of Caching Systems: Properly configure caching plugins such as WP Rocket (paid) or W3 Total Cache to store static versions of your pages, reducing server load. At cdmon, we provide a server-side caching tool (Varnish) optimized for WordPress installations. How to use the cdmon cache tool.
 
From the cdmon control panel, you have access to tools and services designed to optimize your hosting performance, such as our Varnish Cache tool that you can enable or disable at any time. You can also upgrade to a newer PHP version to achieve maximum optimization.
 
Optimizing wc-ajax=get_refreshed_fragments requests and other AJAX operations in WooCommerce is crucial to keeping your site fast and efficient. By following these practical solutions, you can significantly improve the performance of your online store, ensuring an optimal user experience and better overall site performance.
 
 
For more information, you can contact us.
    • Related Articles

    • Frequent Woocommerce errors and how to fix them

      Online sales are currently on the rise, and the demand for having an eCommerce store for any physical business is now a reality. WordPress is an application that allows you to build all kinds of websites, and of course, it also enables you to create ...
    • How to reduce consumption wp-admin/admin-ajax.php

      Sometimes our WordPress installation may make excessive use of admin-ajax.php, consuming a large amount of the server’s available resources and overloading it. This file comes by default with our WordPress installation as it is part of the core, and ...
    • What to do in case of overconsumption in our accommodation

      When the applications you have hosted generate high server resource consumption, it can affect the rest of the websites hosted on the same machine. For this reason, when cdmon technicians detect this consumption, they inform you via email. This ...
    • Information and causes: Error_Connection_Reset

      The "connection reset" error is an error message that appears in the browser when you try to access a web page and the connection is unexpectedly interrupted before the page finishes loading. This error message can appear for various reasons, but it ...
    • How to change the Wordpress administrator password from the database

      The access credentials to the WordPress administrator are stored in the database assigned to it, so it is possible to modify them by following several steps: First of all, we must access phpMyAdmin from our control panel to manage the WordPress ...