【问题标题】:Send an email notification to customer when a specific coupon code is applied in WooCommerce在 WooCommerce 中应用特定优惠券代码时向客户发送电子邮件通知
【发布时间】:2021-03-09 18:26:49
【问题描述】:

在客户结账时使用了特定的促销代码“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 项目,非常感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce checkout coupon


    【解决方案1】:

    不需要使用全局变量,通过$user_id可以得到WC_Customer实例对象,然后是账单邮箱

    • wp_mail() - 发送邮件,类似于 PHP 的邮件功能

    所以你得到:

    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 );
    
                wp_mail( $to, $subject, $content );
            }
        }
    }
    add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );
    

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 2020-11-24
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 2021-02-08
      • 2014-11-26
      • 2019-04-24
      • 2021-01-05
      相关资源
      最近更新 更多