Check if WooCommerce is Active

Over the past couple of years, I have written and shared number of snippets on WooCommerce which I hope helped some of you. Generally speaking, when I am sharing a snippet on WooCommerce, I do that with an assumption that you already have WooCommerce installed and activated on your site. Because those snippets were specifically written for WooCommerce and it would be useless without having the plugin installed and activated already!

Running those snippets on your active theme's functions.php page when you do not have WooCommerce activated may cause issues. Though I personally didn't test them all out on "what if WooCommerce is not activated" situation in mind. It's our shared responsibility to make them fail-safe. Especially for those who are developing theme's for others.

I am pretty sure many of you already know that but you should always wrap your snippet within a conditional statement to make sure your WooCommerce snippets runs only if you have WooCommerce activated on your system. Here is what you should always do regardless of whether I mention them on my snippet or not.

if (class_exists('WooCommerce')) {
  // write all the snippets within this block
  // that deals with WooCommerce related issues.
}

As I mentioned within the statement, you should always use all of your WooCommerce related snippets inside this "if" statement. WooCommerce also suggests this practice to avoid fatal error messages on your site and so am I. This way you would have a fully functional site even if WooCommerce is not available.

Not quite sure why exactly I never wrote on this issue in the past! Honestly though, it's just too basic idea that I thought almost all of you know that already. However, I was proven wrong recently by one the user of this site.

Comments

Commenting is disabled.