在 WooCommerce 结帐页面上并在购物车中有产品时向用户发送电子邮件通知

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

我需要能够在用户结帐时向他发送电子邮件,基本上是在即将进行购买时。

我将此代码添加到我的 functions.php 但是,它碰巧以不稳定的方式发送邮件超过 1 次,我认为这是由于我声明的条件,但我不太当然。

代码如下:

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');

woocommerce_after_shop_loop_item 钩子与结帐页面无关,而是在商店页面上执行,您应该改用仅适用于结帐页面的钩子,例如woocommerce_before_checkout_form

为避免错误信息,最好在使用前检查WC->cart是否实际可用

所以你得到:

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' );

可选: 发送与其他 WooCommerce 电子邮件通知布局相同的电子邮件通知

替换

// 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 );
}

// 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 );
}