From 791b4db8f39d3e866186b6d169f1fb571d241b1e Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 15 Oct 2024 13:32:46 -0300 Subject: [PATCH] fix(modal-checkout): tweak Woo Payments behavior --- includes/class-modal-checkout.php | 49 +++++++++++++++++++++++++++++++ src/modal-checkout/index.js | 8 ++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index b7b3d52b6..60796f6f9 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -86,6 +86,8 @@ public static function init() { add_action( 'option_woocommerce_default_customer_address', [ __CLASS__, 'ensure_base_default_customer_address' ] ); add_action( 'default_option_woocommerce_default_customer_address', [ __CLASS__, 'ensure_base_default_customer_address' ] ); add_action( 'wp_ajax_process_name_your_price_request', [ __CLASS__, 'process_name_your_price_request' ] ); + add_filter( 'option_woocommerce_woocommerce_payments_settings', [ __CLASS__, 'filter_woocommerce_payments_settings' ] ); + add_action( 'init', [ __CLASS__, 'unhook_woocommerce_payments_update_billing_fields' ] ); /** Custom handling for registered users. */ add_filter( 'woocommerce_checkout_customer_id', [ __CLASS__, 'associate_existing_user' ] ); @@ -308,6 +310,53 @@ function ( $item ) { } } + /** + * Filter unsupported Woo Payments features. + * + * @param array $settings WooCommerce Payments settings. + * + * @return array Filtered WooCommerce Payments settings. + */ + public static function filter_woocommerce_payments_settings( $settings ) { + if ( ! self::is_modal_checkout() ) { + return $settings; + } + if ( isset( $settings['platform_checkout'] ) ) { + $settings['platform_checkout'] = 'no'; + } + return $settings; + } + + /** + * Unhook WooCommerce Payments billing fields update. + * + * The WC_Payment_Gateway_WCPay->checkout_update_email_field_priority() hook + * changes the position of the email address field in the checkout form and + * appends a broken Stripelink button to the email input. + */ + public static function unhook_woocommerce_payments_update_billing_fields() { + if ( ! self::is_modal_checkout() ) { + return; + } + if ( ! class_exists( 'WC_Payments' ) ) { + return; + } + $gateway = \WC_Payments::get_gateway(); + if ( ! $gateway ) { + return; + } + $filters = $GLOBALS['wp_filter']['woocommerce_billing_fields']; + foreach ( $filters as $index => $filter ) { + $keys = array_keys( $filter ); + foreach ( $keys as $key ) { + if ( strpos( $key, 'checkout_update_email_field_priority' ) !== false ) { + remove_filter( 'woocommerce_billing_fields', $key, $index ); + } + } + } + } + + /** * Process name your price request for modal. */ diff --git a/src/modal-checkout/index.js b/src/modal-checkout/index.js index de7e73c12..9678cba73 100644 --- a/src/modal-checkout/index.js +++ b/src/modal-checkout/index.js @@ -602,7 +602,13 @@ import { domReady } from './utils'; $genericErrors.remove(); } - const serializedForm = $form.serializeArray(); + const removeFromValidation = [ + 'save_user_in_woopay', + ]; + // Serialize form and remove fields that shouldn't be included for validation. + const serializedForm = $form.serializeArray().filter( + item => ! removeFromValidation.includes( item.name ) + ); // Add 'update totals' parameter so it just performs validation. serializedForm.push( { name: 'woocommerce_checkout_update_totals', value: '1' } ); // Ajax request.