在管理新订单电子邮件模板中添加应用的优惠券代码 - WooCommerce

问题描述 投票:0回答:2

让我澄清我的问题:

  • 我已下载并激活用于电子商务功能的 WooCommerce 插件。
  • 我想使用我的自定义插件在管理新订单电子邮件模板中添加“已应用的优惠券代码”。

现在:

  1. 您能告诉我实际设置新订单电子邮件模板以便我覆盖它的确切钩子或函数吗?
  2. 你能告诉我如何调用应用的优惠券代码,以便我将其显示在电子邮件模板中吗?

如果你能帮助我,那就太好了。

php wordpress woocommerce coupon email-notifications
2个回答
8
投票

这可以使用挂接到 woocommerce_email_order_details 操作挂钩(例如)中的自定义函数来完成,该函数将在管理电子邮件通知中显示订单中使用的优惠券:

// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
    
    // Only for admins and when there at least 1 coupon in the order
    if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;

    foreach( $order->get_items('coupon') as $coupon ){
        $coupon_codes[] = $coupon->get_code();
    }
    // For one coupon
    if( count($coupon_codes) == 1 ){
        $coupon_code = reset($coupon_codes);
        echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
    } 
    // For multiple coupons
    else {
        $coupon_codes = implode( ', ', $coupon_codes);
        echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'</p>';
    }
}

代码位于活动子主题(或主题)的 function.php 文件中,或者也位于任何插件文件中。

已测试并有效...


0
投票

这是因为

$coupon_codes
不是一个 array() 用
$coupon_codes=array();

定义它
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.