11

I'm creating a theme with woocommerce which has the following:

<ul>

  <li>
    <?php 
    if( is_user_logged_in() ){ 
    ?>
      <a href="<?php echo get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ); ?>" title="<?php _e( 'My Account', 'woothemes' ); ?>"><?php _e( 'My Account', 'woothemes' ); ?></a>
    <?php } else { ?>
      <a href="<?php echo get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ); ?>" title="<?php _e( 'Sign-in', 'woothemes' ); ?>"><?php _e( 'Sign-in', 'woothemes' ); ?></a>
    <?php 
    } 
    ?>
  </li>

  <li>
    <a href="<?php echo get_option( 'home' ) . '/' . 'my-lists/' ?>" title="My Wishlist">Wishlist</a>
  </li>

  <!--
  <li><a href="#" title="Email updates">Email Updates</a></li>
  -->
  <li> 
    <?php 
    global $woocommerce; 
    ?>
    <a id="header-ShoppingCart" class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart', 'woothemes' ); ?>"><?php echo sprintf( _n( '%d item', 'My Bag (%d)', $woocommerce->cart->cart_contents_count, 'woothemes' ), $woocommerce->cart->cart_contents_count ); ?> - <span id="header-cartCount"><?php echo $woocommerce->cart->get_cart_total(); ?></span></a>
  </li>

</ul>

However this breaks the site if the woocommerce plugin is not activated. How can I check if the woocommerce plugin is activated?

6 Answers 6

35

You could check to see if the 'WooCommerce' class exists, then run the code that requires WooCommerce.

<?php
if ( class_exists( 'WooCommerce' ) ) {
  // code that requires WooCommerce
} else {
  // you don't appear to have WooCommerce activated
}
?>
7
  • 2
    class_exists('WooCommerce') is going to return false, if the code is placed in functions.php while "activating plugin" ( the plugin is going to be activated after you check for WooCommerce class ).
    – pyronaur
    Jan 28, 2015 at 11:58
  • So this would be false if someone happened to load the page at the exact moment that the plugin was being activated? Jan 28, 2015 at 14:49
  • 1
    You can't really load at the "exact moment", you can load 1ms before or after. It's important to pay attention here, because, if you declare some Woo function for a condition where WooCommerce doesn't exist ( like your own is_woocommerce() ), then you will receive a fatal error when trying to activate WooCommerce Plugin, because you will register your function first, and only then WooCommerce is going to try to register their is_woocommerce() function, and that is going to cause a fatal error and prevent WooCommerce plugin from ever activating.
    – pyronaur
    Jan 29, 2015 at 16:14
  • 1
    Right, but my point is that it would only fail if you declared a function that was also declared in WooCommerce. Simply detecting it's existence in a theme, as stated in the question, wouldn't be affected by this. Jan 30, 2015 at 15:37
  • 1
    Yep, And I'm not arguing with you, I'm just posting relevant information, just in case someone decides to redeclare a function if WooCommerce doesn't exist
    – pyronaur
    Feb 5, 2015 at 9:38
12

To check if woocommerce or anyother plguin is active, paste the following code on the template where you want to display the message.

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    echo 'WooCommerce is active.';
} else {
    echo 'WooCommerce is not Active.';
}
3
9

Use following code. It will work on multi site also.

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'woocommerce/woocommerce.php') ) {
  // Do what you want in case woocommerce is installed
}
1
  • As of now this should be the accepted answer.
    – Aleksandar
    Jan 7, 2021 at 17:12
0

I would perform the class check once all plugins are loaded. Because in some cases it can return false negative.

add_action( 'plugins_loaded', 'see_all_plugins_loaded' );

function see_all_plugins_loaded() {
   if ( class_exists( 'WooCommerce' ) ) {
       // do your stuff
   }
 }
0

The new official way to do it with multisite support:

// Test to see if WooCommerce is active (including network activated).
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'woocommerce/woocommerce.php';

if (
    in_array( $plugin_path, wp_get_active_and_valid_plugins() )
    || in_array( $plugin_path, wp_get_active_network_plugins() )
) {
    // Custom code here. WooCommerce is active, however it has not 
    // necessarily initialized (when that is important, consider
    // using the `woocommerce_init` action).
}
0

I use:

$is_woo = (function_exist('is_woocommerce') || class_exists( 'WooCommerce' ) );
if($is_woo){
    // WooCommerce Is Active
}else{
    // WooCommerce Isn't Active
}

you can use this easy method too:

function kdev_is_active_woocommerce(){
    return  (function_exists('is_woocommerce') || class_exists( 'WooCommerce' ) ) || ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) );
}
if(kdev_is_active_woocommerce()){
    // WooCommerce Is Active
}else{
    // WooCommerce Isn't Active
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.