Customizing Woocommerce New Order email notification based on shipping method [closed]

一曲冷凌霜 提交于 2020-01-01 16:47:32

问题


How can I edit the admin-new-order.php WooCommerce template to send conditionally some custom customer details based on shipping method?

For example (for New Order email notification):

  • If the Order Shipping method is flat rate, I want to add some custom fields information to the customer details.
  • If the Order shipping method is NOT flat rate, I don't want custom fields info to be displayed.

Or maybe there's some snippet that goes in function.php for this?


回答1:


You can use a custom function hooked in any of this hooks (tuning the hook priority):

  • woocommerce_email_order_details
  • woocommerce_email_order_meta
  • woocommerce_email_customer_details

Here it is an example of hooked code (instead of overriding the template):

add_action ('woocommerce_email_customer_details', 'custom_email_customer_details', 15, 4);
function custom_email_customer_details( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "New Order" email notification
    if ( 'new_order' != $email->id ) return;

    // Only "Flat Rate" Shipping Method
    if ( $order->has_shipping_method('flat_rate') ){
        $order_id = $order->get_id(); // The Order ID

        // Test output
        echo "<p>Message: The Order ID is $order_id</p>";
        echo '<p>Custom field: '. get_post_meta( $order_id, '_recorded_sales', true ) .'</p>';
    }
}

The hook priority here is 15, so it comes after the customer details.

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

This code is tested and works.



来源:https://stackoverflow.com/questions/46083022/customizing-woocommerce-new-order-email-notification-based-on-shipping-method

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