Skip to content

Commit

Permalink
PIWOO-414 add phone only if optional in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Mar 14, 2024
1 parent ff6cedc commit d0e3405
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
25 changes: 11 additions & 14 deletions resources/js/mollieBlockIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 12 additions & 5 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}
}
}
Expand Down

0 comments on commit d0e3405

Please sign in to comment.