-
Notifications
You must be signed in to change notification settings - Fork 1
KNA-3176/fix: field reset when checkout is cached too early #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,59 @@ public function __construct($wc_twoinc) | |
|
|
||
| // Order pay page customization | ||
| add_action('woocommerce_pay_order_before_submit', [$this, 'order_pay_page_customize'], 24); | ||
|
|
||
| // Fix for checkout field caching conflicts with other plugins (e.g., PPOM, Dokan) | ||
| // Some plugins trigger WC_Checkout::get_checkout_fields() early during wp_loaded, | ||
| // before Two's filters are registered, causing fields to be cached without Two's additions. | ||
| add_action('woocommerce_checkout_init', [$this, 'maybe_clear_checkout_field_cache'], 1); | ||
| } | ||
|
|
||
| /** | ||
| * Clear WooCommerce checkout field cache if Two's fields are missing. | ||
| * | ||
| * This fixes compatibility issues with plugins that trigger checkout field | ||
| * initialization before Two's filters are registered (e.g., PPOM, Dokan). | ||
| * | ||
| * @param WC_Checkout $checkout The checkout instance. | ||
| * @return void | ||
| */ | ||
| public function maybe_clear_checkout_field_cache($checkout) | ||
| { | ||
| if (!$checkout instanceof WC_Checkout) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $reflection = new ReflectionClass($checkout); | ||
|
|
||
| if (!$reflection->hasProperty('fields')) { | ||
| return; | ||
| } | ||
|
|
||
| $fields_property = $reflection->getProperty('fields'); | ||
| $fields_property->setAccessible(true); | ||
| $cached_fields = $fields_property->getValue($checkout); | ||
|
|
||
| // Only clear cache if fields are already set but Two's fields are missing | ||
| if ( | ||
| is_array($cached_fields) && | ||
| isset($cached_fields['billing']) && | ||
| is_array($cached_fields['billing']) && | ||
| !isset($cached_fields['billing']['company_id']) | ||
| ) { | ||
| $fields_property->setValue($checkout, null); | ||
| } | ||
| } catch (ReflectionException $e) { | ||
| // Log the exception for debugging - helps identify issues if WooCommerce internals change | ||
| if (function_exists('wc_get_logger')) { | ||
| wc_get_logger()->warning( | ||
| 'Two: Failed to clear checkout field cache due to ReflectionException: ' . $e->getMessage(), | ||
| ['source' => 'two-payment-gateway'] | ||
| ); | ||
| } | ||
| // Silently continue - cache clearing is a best-effort optimization | ||
| // The checkout will still work, just without Two's custom fields in edge cases | ||
| } | ||
|
Comment on lines
+83
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While silently failing is acceptable to prevent site crashes, logging the } catch (ReflectionException $e) {
if (function_exists('wc_get_logger')) {
wc_get_logger()->warning(
'Two Checkout: Failed to clear checkout field cache due to ReflectionException: ' . $e->getMessage(),
['source' => 'twoinc-payment-gateway']
);
}
// Silently fail...
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated PR |
||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential PHP warnings if
$cached_fields['billing']is set but is not an array, it's safer to add a check withis_array()before attempting to access an index on it. This improves the robustness of the compatibility fix. For example, you could change the condition to something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated PR