How to check WooCommerce Version In PHP

In writing plugins for Woocommerce, it’s generally good to check to make sure it’s installed, activated, and is a version your plugin supports.

I’ve released one plugin and am now writing another and realized that not having Woocommerce could cause your website to break if certain requirements aren’t met.

If you’re also writing something that requires Woocommerce, you’ll also want to check for this kind of stuff. Maybe not if it’s a one-off piece of code, but if you plan to reuse it, definitely include something that checks for the version of Woocommerce.

This below code can be used anywhere in WordPress, but the most common places would be in your functions.php file or in a plugin file. Being in WordPress, it’s written in PHP and is meant for someone who knows a little bit about coding.

Here’s how to check for the Woocommerce version in PHP

Paste this code into your functions.php or plugin files (if you’re writing a plugin).

function nwp_woo_version_check( $version = '3.0' ) {
	if ( ! class_exists( 'WooCommerce' ) ) {
		//woocommerce is not activated or installed
	    return;
	}

		
	if ( version_compare( WC_VERSION, $version, ">=" ) ) {
		//it's installed, activated, and a high enough version
	    return;
	}
	
}
add_action('init', 'nwp_woo_version_check');

Leave a Reply

Your email address will not be published. Required fields are marked *