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
- How to check if the product is in a certain category on a single-product.php in Woocommerce?
- How to delete all records from or empty a custom database table?
- How to create a flexible abstraction for WP_Query?
- Add Plugins to WordPress Theme
- Coding Challenge – JavaScript Easter Egg
- Bug in Stackoverflow styled badge system script
- Run function on plugin upgrade
- Disable plugin on Specific Page of Admin
- How to add nofollow on all external links without plugin?
- Over write plugin templates
- PGP-Encrypt system-generated notifications
- Using has_filter with a class based plugin to check whether the plugin is active or not
- Social sharing post’s shortlink instead of permalink
- Plugin/Folder permission issues with Azure
- Get data from plugin People list in author template
- How to limit WordPress pages during updates?
- WordPress converting my code
- Output array into one table cell
- How to add a full edit link to front-end editor?
- How to Create a Custom Panel and Fields in Post Page [Plugin]
- get_post_type() and WP_QUERY issue
- WordPress Post Visibility Options for Frontend
- Show Sitename on Yoast SEO Title tag [closed]
- Plugins in WP_Options but not Active
- Create a single website with 3 theme options in wordpress
- React error #231with Custom Blocks after upgrade to WP 5.5
- Background Music WP [closed]
- WordPress failing to update plugins
- Image tooltip enable/disable
- Upload files to the plugin menu
- Call another page in forms
- Chat support plugin? [closed]
- how does ‘init’ /’parse_request’ hook work for wordpress?
- Service status plugin
- Share login credential with QR code
- Shortcode do not return the right data in post
- Avoid WordPress header bar when form submission using admin_post in the frontend
- Bootstrap carousel – Using shortcode inside enclosing shortcode
- ClassLoader.php issues after moving to a new host
- plugins break after moving wp-content folder
- How to populate a Mailchimp newsletter with latest events from WordPress plugin EventOn?
- Change localization only for plugin
- How to get author developer link on plugin page
- WordPress 3.5 install error
- Customize Multi-Column Tag Map to fetch specific post-type
- woocommerce retriving category name as div class?
- Shortcode to do math with url variables