Merge pull request #742 from woocommerce/PCP-799-pui-feature-request-add-option-for-a-phone-number-field-next-to-the-birth-date-field

PUI feature request: add option for a phone number field next to the Birth Date field (799)
This commit is contained in:
Emili Castells 2022-08-10 09:20:03 +02:00 committed by GitHub
commit 79acb04e73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View file

@ -333,6 +333,23 @@ class PayUponInvoice {
)
);
$checkout_fields = WC()->checkout()->get_checkout_fields();
$checkout_phone_required = $checkout_fields['billing']['billing_phone']['required'] ?? false;
if ( ! array_key_exists( 'billing_phone', $checkout_fields['billing'] ) || $checkout_phone_required === false ) {
woocommerce_form_field(
'billing_phone',
array(
// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
'label' => __( 'Phone', 'woocommerce' ),
'type' => 'tel',
'class' => array( 'form-row-wide' ),
'validate' => array( 'phone' ),
'autocomplete' => 'tel',
'required' => true,
)
);
}
echo '</div><div>';
// phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
@ -376,6 +393,9 @@ class PayUponInvoice {
}
$national_number = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING );
if ( ! $national_number ) {
$errors->add( 'validation', __( 'Phone field cannot be empty.', 'woocommerce-paypal-payments' ) );
}
if ( $national_number ) {
$numeric_phone_number = preg_replace( '/[^0-9]/', '', $national_number );
if ( $numeric_phone_number && ! preg_match( '/^[0-9]{1,14}?$/', $numeric_phone_number ) ) {

View file

@ -215,6 +215,12 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway {
}
}
$phone_number = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ) ?? '';
if ( $phone_number ) {
$wc_order->set_billing_phone( $phone_number );
$wc_order->save();
}
$wc_order->update_status( 'on-hold', __( 'Awaiting Pay upon Invoice payment.', 'woocommerce-paypal-payments' ) );
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
$payment_source = $this->payment_source_factory->from_wc_order( $wc_order, $birth_date );

View file

@ -24,8 +24,8 @@ class PaymentSourceFactory {
* @return PaymentSource
*/
public function from_wc_order( WC_Order $order, string $birth_date ) {
$address = $order->get_address();
$address = $order->get_address();
$phone = filter_input( INPUT_POST, 'billing_phone', FILTER_SANITIZE_STRING ) ?? $address['phone'] ?: '';
$phone_country_code = WC()->countries->get_country_calling_code( $address['country'] );
$phone_country_code = is_array( $phone_country_code ) && ! empty( $phone_country_code ) ? $phone_country_code[0] : $phone_country_code;
if ( is_string( $phone_country_code ) && '' !== $phone_country_code ) {
@ -44,7 +44,7 @@ class PaymentSourceFactory {
$address['last_name'] ?? '',
$address['email'] ?? '',
$birth_date,
preg_replace( '/[^0-9]/', '', $address['phone'] ) ?? '',
preg_replace( '/[^0-9]/', '', $phone ) ?? '',
$phone_country_code,
$address['address_1'] ?? '',
$address['city'] ?? '',