The Function ca_get_woo_version_number()
As WooCommerce releases updates, your plugin may need to function differently based on which version of WooCommerce your user is using. The function below will grab the current version number of WooCommerce or return null if the plugin is not installed.
function ca_get_woo_version_number() { // If get_plugins() isn't available, require it if ( ! function_exists( 'get_plugins' ) ) require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); // Create the plugins folder and file variables $plugin_folder = get_plugins( '/' . 'woocommerce' ); $plugin_file = 'woocommerce.php'; // If the plugin version number is set, return it if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) { return $plugin_folder[$plugin_file]['Version']; } else { // Otherwise return null return NULL; } }
Use Case
$woo_version = ca_get_woo_version_number(); if ( $woo_version >= 2.1 ) { } else if ( $woo_version < 2.1 ) { } else { // User does not have plugin installed }