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')
);
}
Related Posts:
- Should all plugins be encapsulated in a Class?
- Registering Class methods as hook callbacks
- Get plugin_dir_url() from one level deep within plugin
- Namespaces in WordPress – How do I initiate the main class?
- How do I extend one plugin I’m writing with another I’m writing using classes?
- Adding an admin page – OOP approach not working
- scheduled event not getting executed
- Using the media uploader in a custom plugin
- Trying to get logged-in user data inside php class
- register child class in another plugin
- Check if a class exists within a method
- How to include and use custom class files in plugin?
- Creating a menu page in a Object Oriented developed plugin
- Current user in plugin returns NULL
- How to structure a plugin into multiple files using classes?
- Override WP Class Private Function
- Calling custom plugin class methods from a template
- Getting user roles in plugin files
- Plugin activation hook in an abstract class
- plugin class inheritance. cannot change variable
- Most efficient way to use classes to create admin pages using Settings API
- Basic Object Oriented plugin question
- Using plugin functions/methods within templates
- How to handle cookies from a WordPress plugin on a cached page?
- accessing parent variables in child construct without executing action in parent
- Object method calling for global $wpdb in header.php
- Why is my custom post type not being activated on plug-in activation?
- How to prevent PHP fatal error if plugin hasn’t been enabled and a method is called in theme file?
- get_option / wp_localize_script Not Working in OOP Plug In
- Built a second plugin but it overwrote the first one
- profile_update hook doesn’t works inside a class
- WordPress Object Oriented plugin development [closed]
- How to access OOP plugin function inside themes or other plugin
- Plugin: Hooking up classes that have their own hooks
- WordPress Custom Hook with Class method
- FPDF for creating pdf diplomas
- WordPress plugin blog creation
- Woocommerce functions in custom class, avoid errors
- Get post content inside plugin class method
- How to access classes in theme of a plugin?
- Reorganization of namespaces
- accessibility of an object created in a plugin, from the header
- Creating a register settings class that supports extended classes
- Autoloading a PHP class – working outside, but not inside WordPress plugin
- Why function hooked using object are executing at all time?
- How do add_action and WP_Query
- private functions in plugins
- Plugin won’t activate, fatal error (widget class not found)
- OOP Plugin: Where should I place the action hooks in the class?
- Global Handle to Class unavailable in Plugin?
- Strict Standards: Non-static method in sidebar generator
- Plugin: Custom menu item problem
- Function not being called on form submit, only blank admin-post.php page
- Is it possible to use Classes between WordPress plugins with separate namespaces?
- Best collection of code for your 'functions.php' file [closed]
- Tips for using WordPress as a CMS? [closed]
- Updates for a private plugin?
- Woocommerce override mini-cart.php not working [closed]
- Error: call_user_func_array() expects parameter 1 to be a valid callback
- Library plugin for WordPress [closed]
- Adding Button to Shop Page in WooCommerce [closed]
- Unable to load stylesheets and scripts to plugin settings page
- Licensing of freemium plugin
- How to add rewrite rules and pagination to retrieve attachments files?
- Can I use a plugin for a singular page on my website?
- How to add Internationalization in WordPress using Javascript/React?
- Do widget options need to be escaped widget()?
- creating html reusable blocks via shortcodes
- I want to auto populate data in three fields if one drop down is selected
- Paypal Integration [closed]
- Remove hovercard for only certian gravatars
- Is there a loading priority for login_enqueue_scripts?
- How to apply lazy loading in background images [closed]
- How can I test plugin activation using phpunit?
- Prevent direct access to WordPress plugin assets?
- WordPress custom taxonomy not showing
- Getting a Page via its post-name using WP REST API v2 and Postman
- Software to run on server checking outdated wordpress installations
- Integrating boxtal PHP library into a custom WordPress Plugin
- Test my WordPress Plugin standard or not
- Modifying post titles on pages, archives and single but not dashboard/admin
- Every time I save my plugin options it erases another plugin options
- is there a way to pass a parameter to a add_menu_page function?
- How can I replace content on site generated from plugin without changing plugin
- TranslatePress | secondary language category/sub-category not working correctly “`/%category%/%postname%/“`
- Need to know if my approach to designing a geneaology plug-in for wordpress makes sense
- Live Streaming with WordPress?
- How to change the WordPress Plugin Watson Assistant Chatbot BOX Size?
- How to edit checkout page? [closed]
- How to access custom post meta data from JavaScript
- Add custom field for users
- Conditional required fields for WordPress Contact Form 7
- County Finder form/plugin?
- avatar displays outside of targeted area
- Adding hero images to blog posts
- How wordpress plugin hooks works? [duplicate]
- Gtranslate function with custom menu
- Increased max_upload_filesize, but still can’t upload plugin [closed]
- Plugin question, How to insert a colon in a value box
- Change homepage logo for each site multisite setup