WC()
Returns the main instance of the WooCommerce class. The function is created to prevent the need to access global variables. The WooCommerce class is created based on the Singleton design pattern.
No Hooks.
Returns
WooCommerce. Returns an instance of the WooCommerce class.
Usage
WC();
Examples
#1 Explanation of how the function works
The initial object is formed by calling the function in the main plugin file woocommerce.php:
function WC() {
return WooCommerce::instance();
}
$GLOBALS['woocommerce'] = WC();
For backward compatibility, the class object is placed in the $GLOBALS superglobal array, meaning the two WooCommerce version retrieval constructs are identical:
// Recommended method $version = WC()->version // The old way $version = $GLOBALS['woocommerce']->version;
#2 Demo
The WC() function is a wrapper for the WooCommerce class, so see there for examples of use.
Changelog
| Since 2.1 | Introduced. |
WC() WC code WC 10.7.0
function WC() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
return WooCommerce::instance();
}