Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a Wordpress Woocommerce plugin. On my local environment it works fine but I have problems when adding the plugin to a replica of the prod environment. I am new to wordpress and not very familiar with web dev (I'm a Java programmer).

In the plugin file, I instantiate a class from the Woocommerce plugin package like this:

$coupon = new WC_Coupon($some_code);

In the local environment (php 5.4.10 , Woocommerce 2.0.13, Wordpress 3.6) it's fine. In the production environment (php 5.4.10 , Woocommerce 1.6.5.2, Wordpress 3.4.2) I have the following error:

Fatal error: Class 'WC_Coupon' not found

I have tried including the file where the WC_Coupon class is defined but then the error becomes

Fatal error: Cannot redeclare class WC_Coupon

So what is the proper way to use classes declared in another plugin?

Note: upgrading is not an option at the moment.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You have to check if the class exists, but before that you have to wait that all plugin are loaded: no one can assure that your plugin is loaded after WooCommerce.

For run a code from plugin when all plugin are loaded hook into plugins_loaded hook.

Be aware that you cannot use this hook in a theme, because when theme load that hook was already fired.

add_action('plugins_loaded', 'my_coupon_init');

function my_coupon_init() {
  if ( class_exists('WC_Coupon') ) {
    $coupon = new WC_Coupon($some_code);
    // some code here
  } else {
    add_action('admin_notices', 'wc_not_loaded');
  }
}

function wc_not_loaded() {
    printf(
      '<div class="error"><p>%s</p></div>',
      __('Sorry cannot create coupon because WooCommerce is not loaded')
    );
}
share|improve this answer
    
Not sure if in that case plugins_loaded is the proper hook. One will have to search when exactly that class is available and hook in after that. Anyway +1 –  kaiser Aug 19 '13 at 13:48
    
In this particular case, I am hooking to the deactivation hook register_deactivation_hook( __FILE__, 'deactivate');. So I can't wait. Can I ask wordpress to load the class if it's not loaded yet? –  NathanZ Aug 19 '13 at 14:01
1  
Thanks @kaiser. The WC_Coupon is loaded by __construct of the main WooCommerce class (really by includes() method directly called by __construct) and WooCommerce class is instanziate (as singleton) as soon as plugin is loaded. So plugins_loaded is proper hook in this case ;) –  G. M. Aug 19 '13 at 14:28
    
Anonymous before core loads? Well done Woo, well done. –  kaiser Aug 19 '13 at 14:45
    
@NathanZ Check class_exists( 'WC_Coupon' ). Also please state that you're using this during deactivation... could have saved answering crowd tons of time... –  kaiser Aug 19 '13 at 14:46

The proper way would be :

 if( class_exists('WC_Coupon') ) $coupon = new WC_Coupon($some_code);

It's better to check if class exists before using it, it avoids fatal error if the plugin is disabled.

You can't redeclare a class it's not allowed in PHP.

You can also extend class :

class My_WC_Coupon extends WC_Coupon {
   //some code
   //some hook
}

But most of the time and in this case with WooCommerce you'd better find a hook in documentation that will handle the job.

share|improve this answer
    
+1 for extending that class (if it already exists). –  kaiser Aug 19 '13 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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