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

4
让我明确我的问题:
我已经下载并激活了WooCommerce插件,以实现电子商务功能。
我想使用自定义插件在管理新订单电子邮件模板中添加“应用的优惠券代码”。
现在:
1.您能告诉我确切的钩子或函数是设置新订单电子邮件模板的吗?这样我就可以覆盖它了。
2.您能告诉我如何调用应用的优惠券代码吗?这样我就可以在电子邮件模板中显示它了。
如果你能帮助我,那就太好了。
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不是一个数组,需要用$coupon_codes=array();来定义它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接