Add a replcement billing email if empty for credit card paymen in WooCommerce

77 Views Asked by At

In WooCommerce, we have 2 payments methods, cash on delivery and credit card online.

I have the following problem in Checkout page:
The payment processor requires the email address from the customer, and it's mandatory.
Then we have made the billing email field mandatory. But that makes a big drop in the conversion rate.

So what I would like for the "email" field is:
If the customer doesn't enter his email address, then we should autopopulate the field or the data sent to the payment processor with a generic replacement email like "[email protected]".

And if the customer enters his email address, then everything will go through.

1

There are 1 best solutions below

0
On

Instead, there is a much more effective way: When the order is placed, on order creation, just before the payment, if the billing email is empty and the selected payment method is not COD, we set a temporary generated email address replacement as billing email.
Then, just after the payment, we remove this email replacement.

This temporary email replacement is generated from the customer complete name (or if there is no name, we generate an alphabetical random string) and from a domain name defined in the first function. This way, the payment gateway can't detect that the email is a fake.

The code:

// Utility function that generates an email from customer name
function generate_email_from_name( $order ) {
    $email_domain = 'gmail.com'; // <== HERE set the email domain name

    $email_name   = sanitize_title( $order->get_billing_first_name() );
    $email_name  .= sanitize_title( $order->get_billing_last_name() );
    $email_name   = substr( str_replace( '-', '', $email_name ), 0, 16 );
    
    if ( empty($email_name) ) {
        // Generate an alphabetic string if name is empty
        $email_name = substr(str_shuffle(str_repeat('abcdefghijklmnopqrstuvwxyz', 14)), 0, 14);
    }
    return sanitize_email($email_name.'@'.$email_domain);
}

// Add a replacement email to the order, just before the payment
add_action( 'woocommerce_checkout_create_order', 'auto_fill_empty_billing_email' );
function auto_fill_empty_billing_email( $order) {
    // If email is empty and if the payment method is not COD
    if( ! $order->get_billing_email() && $order->get_payment_method() !== 'cod' ) {
        // The generated billing email replacement
        $replacement_email = generate_email_from_name( $order );
        // Set the replacement billing email
        $order->set_billing_email( $replacement_email );
        $order->update_meta_data('replacement_email', 'YES' ); // Tag the order
    }
}

// Remove the replacement email just after the payment
add_action( 'woocommerce_pre_payment_complete', 'remove_replacement_email'  );
function remove_replacement_email( $order_id ) {
    // Get the order object
    $order = wc_get_order($order_id);

    // If there is a replacement email, we remove it
    if( $order->get_meta('replacement_email') ) {
        $order->set_billing_email('');
        $order->delete_meta_data('replacement_email');
        $order->save();
    }
}

Code goes in functions.php file of your child theme (or in a plugin). It should work.


If needed, here is the code to make the billing email field optional:

// Make billing email optional
add_action( 'woocommerce_billing_fields', 'optional_billing_email_field', 20, 2 );
function optional_billing_email_field($fields){
    $fields['billing_email']['required'] = false;
    return $fields;
}