• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Prevent WooCommerce cart fragments script loading on all pages

Learn in this tutorial how to prevent the WooCommerce cart fragments script from loading on all pages with a simple code snippet.

WooCommerce uses an AJAX script to update the cart without refreshing the page.

You may be wondering, what’s the problem?

The problem is that WooCommerce loads the default cart fragments script on every page. This can slow down the site, especially on a site with a lot of visitors.

In fact, some speed tests often tell you the following URL as a factor to modify:

https://yourwebsite.com/?wc-ajax=get_refreshed_fragments

So let’s see how to get this script to only load on pages where it is really necessary.

Interested? Here’s how to do it:

Snippet to make WooCommerce cart fragments script only load on the necessary pages

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 839 code snippets of this website, login or subscribe here.

It’s as simple as that.

In this case you allow the cart fragment script to be loaded only where it is strictly necessary:

  • Product page
  • Account page
  • Cart page
  • Checkout page

If you are interested in adding another page to the exception you can do so by adding a conditional tag.

Note: If you use a theme that displays the number of items in the cart in the header do not use this snippet. Although you can consider whether it is really necessary. On pages with a lot of traffic it might be a good idea to do without it. 😉

Conclusion

As you can see, with a few lines of code you can prevent the WooCommerce cart fragments script from loading on pages where it is not needed.

Any questions? Let me know in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

PHP Products WooCommerce

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?