Skip to content

Instantly share code, notes, and snippets.

Last active March 1, 2023 14:02
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
[Theming Snippets] Query whether WooCommerce is activated
* Check if WooCommerce is activated
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
function is_woocommerce_activated() {
if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; }
Copy link

class_exists returns boolean, there's no need for IF structure

function is_woocommerce_activated() {
	return class_exists( 'woocommerce' );

Copy link

OlaIola commented Mar 1, 2023

  1. From this function name, it isn't obvious that this function belong to your current theme. I had a confusion when I saw this in usage because I thought that this function is from WooCommerce itself similar to WP core functions such as is_home() e.t.c. and was wondering hot it possible to use if WC will not be active. I believe that each function in your theme should have theme prefix even if you have a namespace because namespace can be easily overlooked.

  2. Right now we are operating through WP() function and I suggest considering if function_exists( 'WC' ) is a more accurate way to indicate that WC is up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment