Get Current WooCommerce Version Number in Plugin

The Function wpbo_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 wpbo_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

Say your plugin creates an error notices, a ability that has been restructured in WooCommerce version 2.1.

Error Statement Pre 2.1


$woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );

Error Statement Version 2.1


wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );

Use the error statement that works for your user



$woo_version = wpbo_get_woo_version_number();

if ( $woo_version >= 2.1 ) {
     wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );
} else if ( $woo_version < 2.1 ) {
     $woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );
} else {
     // User does not have plugin installed
}

Performance Note

In production you should assign the version number to a global constant or class variable.

4 Comments

  1. James Reply

    Thanks Tyler – very useful!

  2. Claudio Sanches Reply

    You can use the function_exists().

    Example:

    if ( function_exists( 'wc_add_notice' ) ) {
    wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );
    } else {
    $woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );
    }

    Or use the constant WOOCOMMERCE_VERSION and the version_compare():

    if ( defined( 'WOOCOMMERCE_VERSION' ) && version_compare( WOOCOMMERCE_VERSION, '2.1', '>=' ) ) {
    wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );
    } else {
    $woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );
    }

    And of course the global variable $woocommerce:


    global $woocommerce;

    if ( is_object( $woocommerce ) && version_compare( $woocommerce->version, '2.1', '>=' ) ) {
    wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ), 'error' );
    } else {
    $woocommerce->add_error( sprintf( __( "This is my custom error", "your-theme-language" ) );
    }

    • WPBackOffice Reply

      Nice suggestions with good use of the WC libraries. The solution I posted has the advantage of working with any plugin you can define in these variables:

      $plugin_folder = get_plugins( ‘/’ . ‘woocommerce’ );
      $plugin_file = ‘woocommerce.php’;

  3. Hinnerk Altenburg Reply

    What about

    WC()->version ?

Leave a reply