Skip to content

Commit

Permalink
PIWOO-414 add phone only if optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Mar 13, 2024
1 parent 6faa795 commit ff6cedc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
13 changes: 12 additions & 1 deletion src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion src/PaymentMethods/Bancomatpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -33,8 +36,9 @@ protected function getOrderIdOnPayForOrderPage()
return wc_get_order($orderId);
}

protected function phoneNumber()
protected function phoneNumber($phoneValue)
{
$phoneValue = $phoneValue ?: '';
?>
<p class="form-row form-row-wide" id="billing_phone_field">
<label for="<?= esc_attr(self::FIELD_PHONE); ?>" class=""><?= esc_html__('Phone', 'mollie-payments-for-woocommerce'); ?>
Expand All @@ -43,7 +47,7 @@ protected function phoneNumber()
<span class="woocommerce-input-wrapper">
<input type="tel" class="input-text " name="<?= esc_attr(self::FIELD_PHONE); ?>" id="<?= esc_attr(self::FIELD_PHONE); ?>"
placeholder="+00000000000"
value="" autocomplete="phone">
value="<?= esc_attr($phoneValue); ?>" autocomplete="phone">
</span>
</p>
<?php
Expand All @@ -53,9 +57,4 @@ public function getFieldMarkup($gateway, $dataHelper)
{
return "";
}

private function isPhoneValid(string $get_billing_phone)
{
return preg_match('/^\+[0-9]{11,13}$/', $get_billing_phone) === 1;
}
}
25 changes: 13 additions & 12 deletions src/PaymentMethods/PaymentFieldsStrategies/In3FieldsStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ public function execute($gateway, $dataHelper)
{
$showBirthdateField = false;
$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());
$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) {
Expand Down Expand Up @@ -56,8 +61,9 @@ protected function dateOfBirth()
<?php
}

protected function phoneNumber()
protected function phoneNumber($phoneValue)
{
$phoneValue = $phoneValue ?: '';
?>
<p class="form-row form-row-wide" id="billing_phone_field">
<label for="<?= esc_attr(self::FIELD_PHONE); ?>" class=""><?= esc_html__('Phone', 'mollie-payments-for-woocommerce'); ?>
Expand All @@ -66,7 +72,7 @@ protected function phoneNumber()
<span class="woocommerce-input-wrapper">
<input type="tel" class="input-text " name="<?= esc_attr(self::FIELD_PHONE); ?>" id="<?= esc_attr(self::FIELD_PHONE); ?>"
placeholder="+00000000000"
value="" autocomplete="phone">
value="<?= esc_attr($phoneValue); ?>" autocomplete="phone">
</span>
</p>
<?php
Expand All @@ -76,9 +82,4 @@ public function getFieldMarkup($gateway, $dataHelper)
{
return "";
}

private function isPhoneValid(string $get_billing_phone)
{
return preg_match('/^\+[0-9]{11,13}$/', $get_billing_phone) === 1;
}
}
1 change: 1 addition & 0 deletions src/Shared/SharedDataDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SharedDataDictionary
'mollie-payments-for-woocommerce_removeOptionsAndTransients',
'mollie-plugin-version',
'mollie-new-install',
'mollie_wc_is_phone_required_flag',
];
public const DB_VERSION_PARAM_NAME = 'mollie-db-version';
public const PLUGIN_VERSION_PARAM_NAME = 'mollie-plugin-version';
Expand Down

0 comments on commit ff6cedc

Please sign in to comment.