Update check-if-woo-is-active.md (#44306)

* Update check-if-woo-is-active.md

Added more reliable methods to check if WooCommerce is active

* Update check-if-woo-is-active.md

Update code examples to use WooCommerce hooks.

* Update check-if-woo-is-active.md

Update wording in some sections

* Update docs/extension-development/check-if-woo-is-active.md

Co-authored-by: Darren Ethier <darren@roughsmootheng.in>

* add docs manifest

---------

Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
Co-authored-by: Jacklyn Biggin <hi@jacklyn.dev>
This commit is contained in:
James Kemp 2024-02-05 20:08:30 +00:00 committed by GitHub
parent 560f0e933d
commit 113a7caa7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 21 deletions

View File

@ -449,7 +449,7 @@
"menu_title": "Check if WooCommerce is active",
"tags": "how-to",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/docs/extension-development/check-if-woo-is-active.md",
"hash": "47cb6a0f60dad691e3dfd1f26e00b5582d51ec23e7de3f800126701b5967f557",
"hash": "dca930ebe334bb3bb276ec912d050830b784ec9f755be491d41c3c32f5b65637",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/docs/extension-development/check-if-woo-is-active.md",
"id": "2c2e89013ad76ff2596680224047723163512bc2"
},
@ -1204,5 +1204,5 @@
"categories": []
}
],
"hash": "19fff439ddbee80f3f6c932d45314d0831a0130c4b9940c40fe8b5f8c581346a"
"hash": "a138874d270595236e1db122206be746424ea22f376a8799473e5505486c4b11"
}

View File

@ -4,28 +4,45 @@ menu_title: Check if WooCommerce is active
tags: how-to
---
You can wrap your plugin in a check to see if WooCommerce is installed:
When developing for WooCommerce, ensuring that WooCommerce is installed and active before your code runs is crucial. This prevents errors related to missing WooCommerce functions or classes.
There are a few methods to achieve this. The first is to execute your code on the `woocommerce_loaded` action. This approach guarantees that WooCommerce and its functionalities are fully loaded and available for use. This is fired around the same time as the core `plugins_loaded` action.
```php
// Test to see if WooCommerce is active (including network activated).
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'woocommerce/woocommerce.php';
if (
in_array( $plugin_path, wp_get_active_and_valid_plugins() )
|| in_array( $plugin_path, wp_get_active_network_plugins() )
) {
// Custom code here. WooCommerce is active, however it has not
// necessarily initialized (when that is important, consider
// using the \`woocommerce_init\` action).
add_action( 'woocommerce_loaded', 'prefix_woocommerce_loaded' );
function prefix_woocommerce_loaded() {
// Custom code here. WooCommerce is active and all plugins have been loaded...
}
```
Note that this check will fail if the WC plugin folder is named anything other than woocommerce.
**Note**: At this stage, WordPress has not yet initialized the current user data.
Another method is to execute your code on the `woocommerce_init` action. This is executed right _after_ WooCommerce is active and initialized. This action (and the `before_woocommerce_init` action) fires in the context of the WordPress `init` action so at this point current user data has been initialized.
```php
add_action( 'woocommerce_init', 'prefix_woocommerce_init' );
function prefix_woocommerce_init() {
// Custom code here. WooCommerce is active and initialized...
}
```
**Note**: The `before_woocommerce_init` hook is also an option, running just _before_ WooCommerce's initialization
Using the above hooks grants access to WooCommerce functions, enabling further condition checks. For instance, you might want to verify WooCommerce's version to ensure compatibility with your code:
```php
add_action( 'woocommerce_init', 'prefix_woocommerce_init' );
function prefix_woocommerce_init() {
// Only continue if we have access to version 8.7.0 or higher.
if ( version_compare( wc()->version, '8.7.0', '<' ) ) {
return;
}
// Custom code here. WooCommerce is active and initialized...
}
```
Choosing the right hook based on your development needs ensures your WooCommerce extensions or customizations work seamlessly and efficiently.