How to check if woocommerce is activated in theme

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

<ul>

  <li>
    <?php 
    if( is_user_logged_in() ){ 
    ?>
      <a href="https://wordpress.stackexchange.com/questions/159177/<?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="https://wordpress.stackexchange.com/questions/159177/<?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' ) . "https://wordpress.stackexchange.com/" . '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?

3 s
3

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
}
?>

Leave a Comment