From d0e340572e2c35ea5a1046d16b8ee2a52d716f53 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 14 Mar 2024 10:12:48 +0100 Subject: [PATCH] PIWOO-414 add phone only if optional in blocks --- resources/js/mollieBlockIndex.js | 25 +++++++++++-------------- src/Gateway/GatewayModule.php | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/resources/js/mollieBlockIndex.js b/resources/js/mollieBlockIndex.js index 641511e5..8f4f9e3c 100644 --- a/resources/js/mollieBlockIndex.js +++ b/resources/js/mollieBlockIndex.js @@ -7,33 +7,30 @@ import molliePaymentMethod from './blocks/molliePaymentMethod' } window.onload = (event) => { const { registerPaymentMethod } = wc.wcBlocksRegistry; + const { checkoutData, defaultFields } = wc.wcSettings.allSettings; + const { billing_address, shipping_address } = checkoutData; const { ajaxUrl, filters, gatewayData, availableGateways } = mollieBlockData.gatewayData; const {useEffect} = wp.element; const isAppleSession = typeof window.ApplePaySession === "function" const isBlockEditor = !!wp?.blockEditor; function getCompanyField() { - let shippingCompany = document.getElementById('shipping-company'); - let billingCompany = document.getElementById('billing-company'); + let shippingCompany = shipping_address.company ?? false; + let billingCompany = billing_address.company ?? false; return shippingCompany ? shippingCompany : billingCompany; } function getPhoneField() { - const shippingPhone = document.getElementById('shipping-phone'); - const billingPhone = document.getElementById('billing-phone'); + const shippingPhone = shipping_address.phone ?? false; + const billingPhone = billing_address.phone ?? false return billingPhone || shippingPhone; } - function isFieldVisible(field) - { - return field && field.style.display !== 'none'; - } - let companyField = getCompanyField(); - const isCompanyFieldVisible = companyField && isFieldVisible(companyField); - const companyNameString = companyField && companyField.parentNode.querySelector('label') ? companyField.parentNode.querySelector('label').innerHTML : false; - let phoneField = getPhoneField(); - const isPhoneFieldVisible = phoneField && isFieldVisible(phoneField); - const phoneString = phoneField && phoneField.parentNode.querySelector('label') ? phoneField.parentNode.querySelector('label').innerHTML : false; + + const isCompanyFieldVisible = getCompanyField(); + const companyNameString = defaultFields.company.label + const isPhoneFieldVisible = getPhoneField(); + const phoneString = defaultFields.phone.label let requiredFields = { 'companyNameString': companyNameString, 'phoneString': phoneString, diff --git a/src/Gateway/GatewayModule.php b/src/Gateway/GatewayModule.php index 5f011187..01b636b2 100644 --- a/src/Gateway/GatewayModule.php +++ b/src/Gateway/GatewayModule.php @@ -7,6 +7,7 @@ namespace Mollie\WooCommerce\Gateway; use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; +use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; use Mollie\WooCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule; use Mollie\WooCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait; use Mollie\WooCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule; @@ -274,8 +275,7 @@ static function () { [$this, 'switchFields'], 11 ); - add_action( 'woocommerce_rest_checkout_process_payment_with_context', [$this, 'addPhoneWhenRest'],11 ); - + add_action('woocommerce_rest_checkout_process_payment_with_context', [$this, 'addPhoneWhenRest'], 11); } $isBancomatPayEnabled = mollieWooCommerceIsGatewayEnabled('mollie_wc_gateway_bancomatpay_settings', 'enabled'); if ($isBancomatPayEnabled) { @@ -295,7 +295,7 @@ static function () { [$this, 'switchFields'], 11 ); - add_action( 'woocommerce_rest_checkout_process_payment_with_context', [$this, 'addPhoneWhenRest'],11 ); + add_action('woocommerce_rest_checkout_process_payment_with_context', [$this, 'addPhoneWhenRest'], 11); } // Set order to paid and processed when eventually completed without Mollie add_action('woocommerce_payment_complete', [$this, 'setOrderPaidByOtherGateway'], 10, 1); @@ -346,13 +346,13 @@ 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) { + 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{ + } else { update_option('mollie_wc_is_phone_required_flag', false); } return $args; @@ -897,6 +897,13 @@ public function addPhoneWhenRest($arrayContext) if ($billingPhone) { $context->order->set_billing_phone($billingPhone); $context->order->save(); + } else { + $message = __('Please introduce a valid phone number. +00000000000', 'mollie-payments-for-woocommerce'); + throw new RouteException( + 'woocommerce_rest_checkout_process_payment_error', + $message, + 402 + ); } } }