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

如何为Woocommerce优惠券通知邮件设置文本颜色与加粗样式

解决WooCommerce优惠券通知邮件添加样式的问题

要给优惠券通知邮件添加颜色和加粗效果,核心是将纯文本邮件改为HTML格式邮件(wp_mail默认发送纯文本,不识别CSS样式),同时使用内联CSS(邮件客户端对外部CSS支持差,内联样式兼容性最优)。

以下是修改后的完整代码:

// For all Woocommerce versions (since 3.0)
add_action( 'woocommerce_checkout_update_order_meta', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order_id ){
    $order = wc_get_order( $order_id );

    $used_coupons = $order->get_used_coupons();

    if( ! empty($used_coupons) ){
        foreach ( $used_coupons as $coupon_code ) {
            $coupon    = new WC_Coupon( $coupon_code ); // WC_Coupon Object
            $recipient = $coupon->get_meta('email_recipient'); // get recipient

            if( ! empty($recipient) ) {
                $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
                // 构建HTML格式邮件内容,用内联CSS实现加粗和颜色效果
                $content = sprintf( 
                    __('The coupon code <span style="font-weight:bold; color:#e74c3c;">"%s"</span> has been applied by a customer'), 
                    $coupon_code 
                );
                
                // 设置邮件头,指定内容为HTML格式
                $headers = array('Content-Type: text/html; charset=UTF-8');
                
                wp_mail( $recipient, $subject, $content, $headers ); // 发送HTML格式邮件
            }
        }
    }
}

关键修改说明:

  • 把邮件内容改为HTML结构,用<span>包裹优惠券代码,通过内联style属性设置font-weight:bold(加粗)和color:#e74c3c(红色,可替换为任意十六进制颜色值)。
  • 添加$headers变量指定邮件内容类型为text/html,确保邮件客户端能正确渲染样式。
  • 保留原有功能逻辑,仅调整邮件内容格式和发送参数。

自定义样式提示:

  • 颜色值可按需替换,比如#2ecc71(绿色)、#3498db(蓝色)等。
  • 还可添加其他内联样式,比如font-size:16px调整字号、text-decoration:underline添加下划线等,多数邮件客户端都支持标准内联CSS属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:41:04