Commit 38d40e99ed for woocommerce
commit 38d40e99edc022cfbb9d19b6515c7a64a580c976
Author: Michael Pretty <prettyboymp@users.noreply.github.com>
Date: Mon Feb 2 14:11:35 2026 -0500
Fix fatal error when REST controllers are instantiated before WooCommerce is loaded (#63066)
* Fix fatal error when REST controllers are instantiated before WooCommerce is loaded
Some third-party plugins (e.g., TikTok for WooCommerce) instantiate WooCommerce
REST API controllers during plugin loading, before WooCommerce has fully
initialized. In WooCommerce 10.5, the RestApiCache trait was added to these
controllers and calls wc_get_container() in initialize_rest_api_cache(), which
is invoked from the constructor. This causes a fatal error because
wc_get_container() doesn't exist yet at plugin load time.
This commit adds a guard to check if wc_get_container() exists before calling
it, preventing the fatal error and maintaining backward compatibility with
plugins that instantiate REST controllers early.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Add changefile(s) from automation for the following project(s): woocommerce
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/63066-fix-rest-api-cache-early-instantiation b/plugins/woocommerce/changelog/63066-fix-rest-api-cache-early-instantiation
new file mode 100644
index 0000000000..488fd3b8bd
--- /dev/null
+++ b/plugins/woocommerce/changelog/63066-fix-rest-api-cache-early-instantiation
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix fatal error when third-party plugins instantiate REST controllers before WooCommerce is fully initialized.
\ No newline at end of file
diff --git a/plugins/woocommerce/src/Internal/Traits/RestApiCache.php b/plugins/woocommerce/src/Internal/Traits/RestApiCache.php
index 781550ef99..644ef5aedc 100644
--- a/plugins/woocommerce/src/Internal/Traits/RestApiCache.php
+++ b/plugins/woocommerce/src/Internal/Traits/RestApiCache.php
@@ -188,6 +188,13 @@ trait RestApiCache {
* @since 10.5.0
*/
protected function initialize_rest_api_cache(): void {
+ // Guard against early instantiation before WooCommerce is fully initialized.
+ // Some third-party plugins instantiate REST controllers during plugin loading,
+ // before the WooCommerce container is available.
+ if ( ! function_exists( 'wc_get_container' ) ) {
+ return;
+ }
+
$features_controller = wc_get_container()->get( FeaturesController::class );
$this->rest_api_caching_feature_enabled = $features_controller->feature_is_enabled( 'rest_api_caching' );