Snippet: Check for the latest WooCommerce version

With WooCommerce 2.1 having just been released, you’ll find that a number of functions that you have been using in your plugins and themes have now been deprecated in favour of better and more aptly named functions. Ideally, it would be great if you could just replace these functions with the new versions, but until everyone upgrades we need to cater for WooCommerce 2.1 as well as 2.0.x. With that in mind here is a simple function that uses PHP’s version_compare() function to check that a site is running the specified version of WooCommerce or higher (defaults to 2.1) along with the basic usage:

<?php
function woocommerce_version_check( $version = '2.1' ) {
if ( function_exists( 'is_woocommerce_active' ) && is_woocommerce_active() ) {
global $woocommerce;
if( version_compare( $woocommerce->version, $version, ">=" ) ) {
return true;
}
}
return false;
}
?>

view raw
function.php
hosted with ❤ by GitHub

<?php
if( woocommerce_version_check() ) {
// Use new, updated functions
} else {
// Use older, deprecated functions
}
?>

view raw
usage.php
hosted with ❤ by GitHub

The function returns boolean true or false and you can specify any WooCommerce version you like for the $version parameter, but leaving it blank will check against v2.1.

To find out which of the functions that you’re using have been deprecated, switch debug mode on and you’ll see all the relevant deprecated notices appear where the functions are being used – these notices should also tell you what functions you can use in place of the old ones.

Leave a Reply