wordpress 发送电子邮件通知,包括订单详细信息时,特定的优惠券代码在WooCommerce中应用

vsdwdz23  于 2023-03-17  发布在  WordPress
关注(0)|答案(1)|浏览(202)

我发现下面的代码是发送一封电子邮件,一旦一个特定的优惠券代码已被使用,但订单的详细信息显然是不可见的内容,因为它只是定义的文本:

add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
    if( $coupon_code == 'bob' ){

        $to = "jack.hoover@gmail.com"; // Recipient
        $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
        $content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );

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

是否可以在内容(消息)中发送订单详细信息,例如{customer_name}、{order_number}或{coupon_amount}?如果不能,如何仅在使用特定优惠券代码时才将新订单发送给其他收件人。感谢您的帮助。
我已经添加了问题中添加的电子邮件变量,但是我想了解它是哪个订单以及订单中的详细信息的触发器应该包括在我的知识范围内,以便让它运行。

qnakjoqk

qnakjoqk1#

是的,有可能,你只需要在代码执行的时候改变钩子,目前它是在cart中执行的,但是你想等待订单创建。

add_action( 'woocommerce_checkout_order_created', 'custom_email_on_applied_coupon_in_order' );
function custom_email_on_applied_coupon_in_order( $order ){
    $send_mail = false;
    // get all applied codes
    $coupon_codes = $order->get_coupon_codes();
    foreach( $coupon_codes as $code )
    {
        if( $code == 'bob' ) $send_mail = true;
    }
    // also check if there is a billing email, when an order is manually created in backend, it might not be filled and the mail will fail
    if( $send_mail && $order->get_billing_email() != '' )
    {
        $to = $order->get_billing_email(); // Recipient
        $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
        $content = sprintf( __('Hello %1$s %2$s, The coupon code "%3$s" has been applied'), $order->get_billing_first_name(), $order->get_billing_last_name(), $coupon_code );
        // you'll find more order details in the $order object, look here how to get it:  https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/  
        wp_mail( $to, $subject, $content );
    }
}

相关问题