Fix/issue WOOTAX-298 - Prevent StoreApi fatal on sites running old WooCommerce#2971
Fix/issue WOOTAX-298 - Prevent StoreApi fatal on sites running old WooCommerce#2971iyut wants to merge 5 commits into
Conversation
|
This works, but I think there's a cleaner way to get the same result and wanted your take. Instead of guarding The upside is it fixes the fatal structurally instead of catching it. On a WooCommerce too old to ship the Store API, We need to confirm What do you think? |
There was a problem hiding this comment.
LGTM Fix is correct, minimal, and follows existing patterns
What works:
- Right placement —
StoreApi::container()atsrc/StoreApi/StoreApiExtendSchema.php:41is the only call site, andextend_store_api()is the only path that reaches it. - Matches codebase style — same
class_exists()pattern, samewc_get_logger()->...+sourcearray used elsewhere in the repo. noticelog level is right — tax still works, only the store-notice extension is missing.- Changelog/readme format matches prior entries.
Concerns:
- No unit test. Easy to add: call
extend_store_api()withoutAutomattic\WooCommerce\StoreApi\StoreApiloaded and assert no fatal. Without it, a future refactor could silently drop the guard. - Guard is call-site-level. Moving it into
StoreApiExtendSchema::__construct()would protect any future caller too. Not blocking, just more robust.
Register the Store API extension on the woocommerce_blocks_loaded action instead of guarding extend_store_api() with a per-request class_exists() check. On WooCommerce versions too old to ship the Store API that action never fires, so the extension is skipped structurally rather than caught, and the class_exists() check is removed. When Blocks have already loaded the extension is registered inline as before. Add unit tests covering both the deferred and already-loaded paths.
…guard Register extend_store_api() on woocommerce_blocks_loaded, the same hook the block integration already uses, instead of calling it during woocommerce_init. The extension only surfaces notices in the block cart/checkout, and on a WooCommerce too old to ship Blocks the hook never fires, so nothing runs. Keep a class_exists() guard on the top-level Automattic\WooCommerce\StoreApi\StoreApi class inside the callback. That covers the in-between versions that ship Blocks (so they fire the hook) but predate that class, which would otherwise still fatal with "Class not found". When the class is absent the method logs a notice and returns. Replace the two deferral tests with tests that exercise the guard itself: registration runs when the class is present, is skipped when it is absent, and is_store_api_available() tracks the real class. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dustinparker, I've looked into WooCommerce commit history and it seems moving the Because And the implementation that @Abdalsalaam has made is looking good to me! Thank you for that! |
Description
WooCommerce Tax was throwing a PHP fatal error on sites running an older WooCommerce version that does not include the
Automattic\WooCommerce\StoreApi\StoreApiclass (introduced when the Blocks StoreApi was merged into WooCommerce core). The plugin's stated minimum WC version (10.7) is advisory — WordPress does not enforce it at runtime — so sites that lagged behind on WooCommerce updates were still loading the plugin and fatalling insideextend_store_api().The fix has two parts:
Register on
woocommerce_blocks_loaded.extend_store_api()now runs on thewoocommerce_blocks_loadedaction (the same hook the block integration already uses at line 704) instead of duringwoocommerce_init. Our extension only surfaces notices in the block cart/checkout, so it only needs to run once Blocks load. On a WooCommerce too old to ship Blocks the hook never fires, so nothing runs and there is no fatal.Guard on
class_exists(). WooCommerce versions between ~5.0 and ~6.3 ship Blocks (so they firewoocommerce_blocks_loaded) but predate the top-levelAutomattic\WooCommerce\StoreApi\StoreApiclass this plugin imports — the hook alone would still fatal there. Soextend_store_api()also guards onclass_exists( '\Automattic\WooCommerce\StoreApi\StoreApi' ). If the class is absent, anoticeis logged viawc_get_logger()and the method returns early. When the class is present the extension registers as before.This addresses the review suggestion to fix the fatal structurally via the hook, while keeping the class check so the in-between versions are covered too.
Related issue(s)
Fixes WOOTAX-298
Steps to reproduce & screenshots/GIFs
Before (to reproduce the fatal):
Automattic\WooCommerce\StoreApi\StoreApiclass (before ~6.4). This includes both WC too old to ship Blocks at all, and WC ~5.0–6.3 that ships Blocks but under the olderBlocks\StoreApinamespace.Uncaught Error: Class "Automattic\WooCommerce\StoreApi\StoreApi" not found in StoreApiExtendSchema.php:41.After:
woocommerce_blocks_loadednever fires, so registration never runs.class_exists()guard is false, and WC logs a notice:StoreApi class not found. Store API extensions will not be registered.Checklist
changelog.txtentry addedreadme.txtentry added