Removing Woocommerce Scripts and CSS from Irrelevant Pages

Originally Posted on February 26, 2014Last Updated on June 6, 2015

If you’re running Woocommerce on a WP site, but don’t want the performance hit that the plugin adds with its multiple Javascript and CSS calls, you can use the code below to remove everything Woo-related from normal index, archive, single post and pages (including the generator tag).

Note that if you’re using Woocommerce widgets or have hardcoded cart information (ie, you show the cart on all pages, not just store pages), your results may vary.

This doesn’t disable features of Woocommerce exactly, Tara. What is does is prevents your site from loading up all of the Javascript and CSS that comes with Woo, so that your non-store pages will load more quickly.

And yes, you should be able to place it anywhere in your functions.php file.

If I use this bit of CSS code, it does indeed remove all the Woocommerce CSS where not required, but when I go to the woocommerce dependent pages, the layout is no longer as it should be (IE the CSS does not appear to be loading)

Andrew removed the code that loaded the woocommerce-smallscreen style sheet, in order to correct the problem of loading it on to a PC or other large screen device. This disabled the small screen style load on any device, making it difficult to use the site on devices with small screens.

Is there a way to modify the code so that the small screen code is loaded only onto small screen devices?

Thanks for the prompt response. I got things to work correctly, but I needed to make a few changes to the code you posted.

I inserted both of the code pieces that you recommended. But, I found that the line of code that registers woocommerce-smallscreen broke all my woocommerce pages; I got http500 pages when trying to access any of them.

I inspected the modified code carefully and found that a “)” and “;” were needed at the end of that piece of code.
Also, I found that the state of the conditional must be set to “is_mobile_device()” instead of “!is_mobile _device()”. With the inversion, the mobile style sheet loads on every device that is not a mobile device. The corrected line is:

Good info – any pointers on how to modify the above code so that the woocommerce geolocation functionality doesn’t get called for a given page? We need this on our site’s store pages, but it’s extremely slow and so we’d of course like to avoid calling it on all of the non-store pages.

For clarity, I’m talking about the “geolocate with page caching support”, that results in URLs with parameters like ?v=11aedd0e4327 appended.