From ff6cedc0cf5d10483b6ff14a830e398757aabd75 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Wed, 13 Mar 2024 17:49:28 +0100 Subject: [PATCH] PIWOO-414 add phone only if optional --- src/Gateway/GatewayModule.php | 13 +++++++++- src/PaymentMethods/Bancomatpay.php | 22 +++++++++++++++- .../BancomatpayFieldsStrategy.php | 21 ++++++++-------- .../In3FieldsStrategy.php | 25 ++++++++++--------- src/Shared/SharedDataDictionary.php | 1 + 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index 2a7d2dce..5f011187 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -346,6 +346,17 @@ static function ($paymentContext) { } ); add_action('add_meta_boxes_woocommerce_page_wc-orders', [$this, 'addShopOrderMetabox'], 10); + add_filter( 'woocommerce_form_field_args', static function ($args, $key, $value) use ($container) { + if ($key !== 'billing_phone') { + return $args; + } + if ($args['required'] === true) { + update_option('mollie_wc_is_phone_required_flag', true); + }else{ + update_option('mollie_wc_is_phone_required_flag', false); + } + return $args; + }, 10, 3); return true; } @@ -873,7 +884,7 @@ public function switchFields($data) private function isPhoneValid($billing_phone) { - return preg_match('/^\+[1-9]\d{1,14}$/', $billing_phone); + return preg_match('/^\+[1-9]\d{10,13}$/', $billing_phone); } public function addPhoneWhenRest($arrayContext) diff --git a/src/PaymentMethods/Bancomatpay.php b/src/PaymentMethods/Bancomatpay.php index dd3d33fe..510aa922 100644 --- a/src/PaymentMethods/Bancomatpay.php +++ b/src/PaymentMethods/Bancomatpay.php @@ -19,7 +19,7 @@ public function getConfig(): array 'products', 'refunds', ], - 'filtersOnBuild' => false, + 'filtersOnBuild' => true, 'confirmationDelayed' => false, 'errorMessage' => __( 'Required field is empty. Phone field is required.', @@ -33,4 +33,24 @@ public function getFormFields($generalFormFields): array { return $generalFormFields; } + + public function filtersOnBuild() + { + add_filter('woocommerce_mollie_wc_gateway_' . $this->getProperty('id') . 'payment_args', function (array $args, \WC_Order $order): array { + return $this->addPaymentArguments($args, $order); + }, 10, 2); + } + /** + * @param WC_Order $order + * @return array + */ + public function addPaymentArguments(array $args, $order) + { + $phone = $order->get_billing_phone(); + if (!empty($phone)) { + $args['billingAddress']['phone'] = $phone; + } + + return $args; + } } diff --git a/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php b/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php index 447ff85d..0952e392 100644 --- a/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php +++ b/src/PaymentMethods/PaymentFieldsStrategies/BancomatpayFieldsStrategy.php @@ -11,18 +11,21 @@ class BancomatpayFieldsStrategy implements PaymentFieldsStrategyI public function execute($gateway, $dataHelper) { $showPhoneField = false; + $isPhoneRequired = get_option('mollie_wc_is_phone_required_flag'); + $phoneValue = false; if (is_checkout_pay_page()) { $order = $this->getOrderIdOnPayForOrderPage(); - $showPhoneField = empty($order->get_billing_phone()) || !$this->isPhoneValid($order->get_billing_phone()); + $phoneValue = $order->get_billing_phone(); + $showPhoneField = true; } - if (is_checkout() && !is_checkout_pay_page()) { - $showPhoneField = true; + if (is_checkout() && !is_checkout_pay_page() && !$isPhoneRequired) { + $showPhoneField = true; } if ($showPhoneField) { - $this->phoneNumber(); + $this->phoneNumber($phoneValue); } } @@ -33,8 +36,9 @@ protected function getOrderIdOnPayForOrderPage() return wc_get_order($orderId); } - protected function phoneNumber() + protected function phoneNumber($phoneValue) { + $phoneValue = $phoneValue ?: ''; ?>

getOrderIdOnPayForOrderPage(); - $showPhoneField = empty($order->get_billing_phone()) || !$this->isPhoneValid($order->get_billing_phone()); $showBirthdateField = true; + $showPhoneField = true; + $order = $this->getOrderIdOnPayForOrderPage(); + $phoneValue = $order->get_billing_phone(); } + if (is_checkout() && !is_checkout_pay_page() && !$isPhoneRequired) { + $showPhoneField = true; + } if (is_checkout() && !is_checkout_pay_page()) { - $showPhoneField = true; - $showBirthdateField = true; + $showBirthdateField = true; } if ($showPhoneField) { - $this->phoneNumber(); + $this->phoneNumber($phoneValue); } if ($showBirthdateField) { @@ -56,8 +61,9 @@ protected function dateOfBirth()