Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions class/WC_Twoinc_Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
) {
Comment on lines +75 to +80

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

$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

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

}

/**
Expand Down