Skip to main content
added 5 characters in body
Source Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

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 thethat hook it'swas 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')
    );
}

You have to check if the class exists, but before that you have to wait that all plugin are loaded: no one can assure 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 the hook it's 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')
    );
}

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')
    );
}
Source Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

You have to check if the class exists, but before that you have to wait that all plugin are loaded: no one can assure 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 the hook it's 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')
    );
}