Add Applied Coupon Code in Admin New Order Email Template - WooCommerce

左心房为你撑大大i 提交于 2020-07-15 03:54:09

问题


Let me clear my question:

  • I have downloaded & activated WooCommerce Plugin for E-Commerce Functionality.
  • I want to add "Applied coupon code" in Admin New Order Email Template using my custom plugin.

Now:

  1. Can you tell me that exact Hook or Function which is actually setting up that New Order Email Template so that i will override it?
  2. Can you tell me how to call applied coupon code, so that i will display this in email template?

It would be great, if you help me please.


回答1:


This can be done using a custom function hooked in woocommerce_email_order_details action hook (for example) that will display in admin emails notifications the used coupons in the order:

// 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>';
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works...




回答2:


This is because $coupon_codes is not an array() define it with $coupon_codes=array();



来源:https://stackoverflow.com/questions/47322228/add-applied-coupon-code-in-admin-new-order-email-template-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!