-
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?
KNA-3176/fix: field reset when checkout is cached too early #312
Conversation
Summary of ChangesHello @SalmanTwo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical compatibility issue where third-party plugins interfere with the proper loading of custom checkout fields by caching the WooCommerce checkout fields too early. The solution introduces a robust fallback mechanism that intelligently detects the absence of required custom fields and clears the checkout field cache, ensuring that all necessary fields are correctly displayed and functional during the checkout process. This directly addresses an issue faced by Prima Profilering. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces a compatibility fix for issues where other plugins cause WooCommerce checkout fields to be cached prematurely, before this plugin's custom fields are added. The solution involves using reflection to clear the cached fields if they are found to be incomplete during checkout initialization. The approach is sound and includes error handling to prevent crashes if the underlying WooCommerce structure changes. I've added a couple of suggestions to improve robustness by adding a more specific type check and to enhance debuggability by logging exceptions.
| if ( | ||
| is_array($cached_fields) && | ||
| isset($cached_fields['billing']) && | ||
| !isset($cached_fields['billing']['company_id']) | ||
| ) { |
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 with is_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:
if (
is_array($cached_fields) &&
isset($cached_fields['billing']) &&
is_array($cached_fields['billing']) &&
!isset($cached_fields['billing']['company_id'])
) {
// ...
}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
| } catch (ReflectionException $e) { | ||
| // Silently fail - cache clearing is a best-effort optimization | ||
| // The checkout will still work, just without Two's custom fields in edge cases | ||
| } |
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.
While silently failing is acceptable to prevent site crashes, logging the ReflectionException would be very helpful for debugging. If the internal structure of WC_Checkout changes in a future WooCommerce update, this functionality will break, and having a log entry would make it much easier to identify the cause. Consider using wc_get_logger() to log the error within the catch block, for example:
} 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...
}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
🖌 Pre-commit success 🏆DetailsExit code: 0 Author ✍️@SalmanTwo |
Disruptive plugins that run at checkout trigger a caching of checkout fields before the Two plugin and custom fields we require for proper use resulting in fields such as company search and selection being disabled.
This fix adds a fallback with safe failing when our required fields are missing when the checkout is already loaded.
In response to issue faced by Prima Profilering