在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知 [英] Send an email notification to customer when a specific coupon code is applied in WooCommerce

查看:0
本文介绍了在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在顾客结账时使用了特定的促销代码‘FREECLASS’后,我正在尝试向他发送电子邮件。

我所做的是在注册后向客户发送代码‘FREECLASS’。我希望客户在使用该代码后收到额外的自定义消息。

基于Send an email notification when a specific coupon code is applied in WooCommerce答案代码,这是我到目前为止所做的,但它不起作用。

add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
    if( $coupon_code == 'FREECLASS' ){
    
    
    // Get user billing email
    global $user_login;
    $user = get_user_by('login', $user_login );
    $email = $user->billing_email;
    
    
        $to = "$email"; // Recipient
        $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
        $content = sprintf( __('The coupon code "%s" has been applied'), $coupon_code );

        wp_mail( $to, $subject, $content );
    }
}

这是我的第一个WooCommerce项目,如果能得到一些帮助,我将不胜感激。

推荐答案

无需使用全局变量,通过$user_id即可获取WC_Customer实例对象,然后获取计费邮箱地址

  • wp_mail()-发送电子邮件,类似于PHP的mail函数

因此您得到:

function action_woocommerce_applied_coupon( $coupon_code ) {
    // NOT logged in, return
    if ( ! is_user_logged_in() ) return;
    
    // Compare
    if ( $coupon_code == 'freeclass' ) {
        // Get user ID
        $user_id = get_current_user_id();
        
        // Get the WC_Customer instance Object
        $customer = New WC_Customer( $user_id );
        
        // Billing email
        $email = $customer->get_billing_email();
        
        // NOT empty
        if ( ! empty ( $email ) ) {
            // Recipient
            $to = $email;
            $subject = sprintf( __('Coupon "%s" has been applied', 'woocommerce' ), $coupon_code );
            $content = sprintf( __('The coupon code "%s" has been applied by a customer', 'woocommerce' ), $coupon_code );
            $headers = array( 'Content-Type: text/html; charset=UTF-8' );

            wp_mail( $to, $subject, $content, $headers );
        }
    }
}
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );

这篇关于在WooCommerce中应用特定优惠券代码时向客户发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆