Skip to content

Conversation

@SalmanTwo
Copy link

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

@SalmanTwo SalmanTwo requested a review from brtkwr January 16, 2026 11:53
@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • Checkout Field Caching Fix: Implemented a mechanism to address issues where WooCommerce checkout fields are cached prematurely by other plugins, leading to the omission of essential custom fields like 'company_id'.
  • Dynamic Cache Clearing: Introduced a new method that checks if required custom fields are missing from the cached checkout fields and, if so, clears the cache to allow for proper field re-initialization.
  • PHP Reflection for Field Access: Utilized PHP Reflection to safely access and manipulate the private fields property of the WC_Checkout object, ensuring compatibility without direct property modification.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 75 to 79
if (
is_array($cached_fields) &&
isset($cached_fields['billing']) &&
!isset($cached_fields['billing']['company_id'])
) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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'])
) {
    // ...
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated PR

Comment on lines 82 to 85
} 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
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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...
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated PR

@github-actions
Copy link

🖌 Pre-commit success 🏆

Details
[INFO] Initializing environment for https://github.com/pre-commit/mirrors-prettier.
[INFO] Initializing environment for https://github.com/pre-commit/mirrors-prettier:prettier@3.1.0.
[INFO] Initializing environment for https://github.com/two-inc/git-hooks.
[INFO] Installing environment for https://github.com/pre-commit/mirrors-prettier.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
prettier.................................................................Passed

Exit code: 0

Author ✍️@SalmanTwo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants