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?
- WordPress Action Hook inside Classes
- WordPress won’t allow for updates to plugins or WordPress Core
- WordPress Stock Update Programatically
- Loading a plugin’s js file from functions.php
- Woocommerce Backend Search by Title and SKU
- I Setup Rate My Post Plugin On Site But Now Got Issues In Google Webmasters
- How to integrate together a website currently hosted WordPress.com and a custom web application currently hosted on Azure?
- How-to: This block can only be used once
- output html on post or page from custom plugin [closed]
- Custom Post Type – custom form in dashboard
- Problem with add_action
- To perform the requested action, WordPress needs to access your web > server
- Custom API plugin to execute 3rd party API to retrieve data
- Is there a way to convert shortcodes to html content?
- Proper way to handle admin-ajax calls
- How to display the featured post on the category page?
- Disable globally “Crop thumbnail to exact dimensions (normally thumbnails are proportional)” with Multisite
- How to find the list of custom post type where logged in user is author
- UTF-32be error WordPress
- Loop in elementor custom widget not working
- Change text string in a plugin
- Set Multiple Meta Values as an Array Using dispatch( ‘core/editor’ ).editPost() Call in Gutenberg/JS
- wp-admin send 404 error
- How to create plugin/ page that reads from database
- Stop wordpress from requesting external jquery from googleapi
- Can the benefits of performance optimization plugins outweigh the tax of installing them on performance?
- get_users(); Is an Undefined Function When Used in Cron Job
- Alternate email sending service – eg: AWS SES [closed]
- 5 PHP Fatal error: Uncaught ArgumentCountError (Cannot Find)
- Disabling a plugin lead to styles being shown as plain text
- the_content() printing DOCTYPE, and tags in the middle of page
- How to deal with Slow HTTP POST (slowloris) vulnerability
- How to create author profile showcase in wordpress
- What is the purpose of WP_CONTENT_URL?
- Blog previews on custom page with more buttons
- Order posts by meta key ( Using ACF )
- Control page content visibility based on URL parameter
- Where does MonsterInsights put Google Analytics code?
- WooCommerce Shopping Cart Not Initially Showing Products [closed]
- Elementor Custom Control: How to get input value?
- Elementor page builder plugin not loading up all the way? [closed]
- How to add fields in the WordPress editor?
- How can I fetch data from another website to my wordpress website with mysql database
- Sucuri SiteCheck failing on domain with iThemes Security Pro plugin
- How to translate WP Blog (search, category etc.)?
- WordPress SVN UTF-8 issue