You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce特定优惠券使用时含订单详情的邮件通知实现问询

问题解答

核心结论

直接在woocommerce_applied_coupon钩子中无法获取{order_number}——因为这个钩子触发时用户还处于购物车阶段,订单尚未创建。但可以获取部分用户、优惠券及购物车信息;如果需要完整订单详情(订单号、最终订单金额等),必须切换到订单生成后的钩子。


方案1:购物车阶段应用优惠券时发邮件(含用户/优惠券/购物车信息)

如果不需要订单号,仅需在用户使用优惠券时即时通知,可以修改原代码,获取当前用户信息、优惠券折扣金额及购物车商品:

add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon_with_details', 10, 1 );
function custom_email_on_applied_coupon_with_details( $coupon_code ){
    // 仅针对指定优惠券触发
    if( $coupon_code !== 'bob' ) return;

    // 获取当前用户名称(登录用户取昵称,访客显示“访客”)
    $current_user = wp_get_current_user();
    $customer_name = $current_user->exists() ? $current_user->display_name : '访客';

    // 获取优惠券对象及优惠金额
    $coupon = new WC_Coupon( $coupon_code );
    $coupon_amount = wc_price( $coupon->get_amount() );

    // 组装邮件内容
    $to = "jack.hoover@gmail.com";
    $subject = sprintf( __('优惠券 "%s" 已被使用'), $coupon_code );
    $content = sprintf( __('客户 "%s" 在购物车中使用了优惠券 "%s",优惠金额:%s'), $customer_name, $coupon_code, $coupon_amount );
    $content .= "\n\n当前购物车商品:";
    
    // 遍历购物车商品添加到邮件
    foreach ( WC()->cart->get_cart() as $item ) {
        $product = $item['data'];
        $content .= "\n- " . $product->get_name() . " × " . $item['quantity'];
    }

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

方案2:订单生成后发含完整详情的通知(支持额外收件人)

如果需要{order_number}、最终订单金额等完整信息,需使用订单创建后的钩子(如woocommerce_new_order),检查订单是否使用了指定优惠券后发送邮件,还可直接复用WooCommerce默认新订单模板:

add_action( 'woocommerce_new_order', 'custom_email_on_order_with_target_coupon', 10, 1 );
function custom_email_on_order_with_target_coupon( $order_id ){
    $order = wc_get_order( $order_id );
    $applied_coupons = $order->get_coupon_codes();

    // 检查订单是否使用了目标优惠券
    if( !in_array( 'bob', $applied_coupons ) ) return;

    // 获取完整订单详情
    $customer_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
    $order_number = $order->get_order_number();
    $coupon = new WC_Coupon( 'bob' );
    $coupon_amount = wc_price( $coupon->get_amount() );
    $order_total = wc_price( $order->get_total() );

    $extra_recipient = "jack.hoover@gmail.com";

    // 自定义邮件内容方案
    $subject = sprintf( __('订单 %s 使用了优惠券 "%s"'), $order_number, 'bob' );
    $content = sprintf( __('客户:%s'), $customer_name ) . "\n";
    $content .= sprintf( __('订单号:%s'), $order_number ) . "\n";
    $content .= sprintf( __('优惠券 "%s" 优惠金额:%s'), 'bob', $coupon_amount ) . "\n";
    $content .= sprintf( __('订单总金额:%s'), $order_total ) . "\n\n";
    $content .= "订单商品详情:\n";
    
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        $content .= "- " . $product->get_name() . " × " . $item->get_quantity() . " 单价:" . wc_price( $item->get_subtotal() / $item->get_quantity() ) . "\n";
    }

    wp_mail( $extra_recipient, $subject, $content );

    // 【可选】发送WooCommerce默认新订单邮件给额外收件人
    // $mailer = WC()->mailer();
    // $new_order_email = $mailer->get_emails()['WC_Email_New_Order'];
    // // 追加额外收件人到默认收件人列表
    // $new_order_email->recipient = $new_order_email->recipient . ',' . $extra_recipient;
    // $new_order_email->trigger( $order_id );
}

方案选择建议

  • 若需即时通知(用户刚用优惠券就触发):选方案1,缺点是无订单号;
  • 若需完整订单数据:选方案2,订单创建后触发,能获取所有订单详情,还可复用官方邮件模板。

内容的提问来源于stack exchange,提问作者Digital Summiteer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:07:10