Sunday, March 13, 2022

[FIXED] Send email notification to user when on WooCommerce checkout page and has products in cart

Issue

I need to be able to send an email to the user when he is in the checkout, basically when is about to make the purchase.

I added this code to my functions.php but, it happens that it sends the mail more than 1 time in an erratic way, I think it is due to the conditionals i declared but i'm not too sure.

Here is the code:

if (is_user_logged_in() && !WC()->cart->is_empty()) {

        /*the current user data*/
        $current_user = wp_get_current_user();
        $email = $current_user->user_email;
        $name = $current_user->user_firstname;

        /*the mail structure*/
        $to = $email;
        $subject = "¡Hola - " . $name . "!, tu compra está casi lista.";
        $body = '
                    //the mail body
        ';

        $headers = array('Content-Type: text/html; charset=UTF-8');
        wp_mail($to, $subject, $body, $headers);
    } /*endif*/
}

add_action('woocommerce_after_shop_loop_item', 'send_mail_when_is_in_checkout');

Solution

The woocommerce_after_shop_loop_item hook has nothing to do with the checkout page but is executed on the shop page, you should instead use a hook that only applies to the checkout page, e.g. woocommerce_before_checkout_form

To avoid error messages, it is best to check whether WC->cart is actually available before using it

So you get:

function action_woocommerce_before_checkout_form() {
    // Only logged in users
    if ( ! is_user_logged_in() ) return;

    // WC Cart
    if ( WC()->cart ) {
        // Cart NOT empty
        if ( ! WC()->cart->is_empty() ) {
            // The current user
            $current_user = wp_get_current_user();
            $email = $current_user->user_email;
            $name = $current_user->user_firstname;

            // NOT empty
            if ( ! empty ( $email ) && ! empty ( $name ) ) {
                // The mail structure
                $to = $email;
                $subject = sprintf( __( 'Hello %s, your message', 'woocommerce' ), $name );
                $body = __( 'The mail body', 'woocommerce' );

                // Headers
                $headers = array( 'Content-Type: text/html; charset=UTF-8' );

                // Sends an email, similar to PHP’s mail function
                wp_mail( $to, $subject, $body, $headers );
            }
        }
    }
}
add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form' );

Optional: to send email notifications with the same layout of the other WooCommerce email notifications

Replace

// NOT empty
if ( ! empty ( $email ) && ! empty ( $name ) ) {
    // The mail structure
    $to = $email;
    $subject = sprintf( __( 'Hello %s, your message', 'woocommerce' ), $name );
    $body = __( 'The mail body', 'woocommerce' );

    // Headers   
    $headers = array( 'Content-Type: text/html; charset=UTF-8' );

    // Sends an email, similar to PHP’s mail function
    wp_mail( $to, $subject, $body, $headers );
}

With

// NOT empty
if ( ! empty ( $email ) && ! empty ( $name ) ) {
    // Mailer
    $mailer = WC()->mailer();
    
    // To, subject, message
    $to = $email;
    $subject = __( 'My subject', 'woocommerce' );
    $message_body = __( 'My message', 'woocommerce' );

    // Message head and message body
    $message = $mailer->wrap_message( sprintf( __( 'Hello %s', 'woocommerce' ), $name ), $message_body );
    
    // Headers
    $headers = 'Content-Type: text/html\r\n';

    // Send an email
    $mailer->send( $to, $subject, $message, $headers );
}


Answered By - 7uc1f3r

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.