Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active March 1, 2023 14:02
Show Gist options
  • Save woogists/fdf6e519d9c30d98c9878fbcbd3ba92a to your computer and use it in GitHub Desktop.
Save woogists/fdf6e519d9c30d98c9878fbcbd3ba92a to your computer and use it in GitHub Desktop.
[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; }
}
}
@AndreiIgna
Copy link

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

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

@OlaIola
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