3

I am using Wordpress 4.9.6 and WooCommerce version 3.4.3 and I need to send 'Order on Hold' email for a specific shipping method.

Reason? I use DHL shipping plugin to calculate shipping and an 'Alternate' shipping method is also available. If the user chooses DHL shipping while checking out, the shipping cost is calculated and the order is good to go. However, if they choose the 'Alternate' shipping method, I got to inform them that their order is on hold till they pay for shipping because the 'Alternate' method is 'Free Shipping' renamed and I will issue a separate invoice for them to pay for shipping once they have ordered.

Searching for a solution to my problem, I have found some code that matches my needs in this answer thread: Customizing Woocommerce New Order email notification based on shipping method

But I am unable to figure out how to edit this code in order to make it work for my specific scenario.

Your help is greatly appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Hamza Ahmad
  • 512
  • 2
  • 7
  • 32
  • 1
    Please, when you use some code from StackOverFlow, you should always mention the linked source and should not include it in your question if you have not made any change on it. – LoicTheAztec Jun 24 '18 at 09:03
  • @LoicTheAztec Sure, will keep that in mind. Thanks – Hamza Ahmad Jun 24 '18 at 09:27

1 Answers1

3

To make it work for a renamed free shipping method, you will need to change the code a bit:

add_action ('woocommerce_email_order_details', 'custom_email_notification_for_shipping', 5, 4);
function custom_email_notification_for_shipping( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "On hold" email notification and "Free Shipping" Shipping Method
    if ( 'customer_on_hold_order' == $email->id && $order->has_shipping_method('free_shipping') ){
        $order_id = $order->get_id(); // The Order ID

        // Your message output
        echo "<h2>Shipping notice</h2>
        <p>Your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here…</p>";
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

enter image description here


Forcing "On hold" and "Completed" email notification (optional)

On orders status change the below code will trigger "On hold" email notification only for your renamed "Free shipping" shipping method and "Completed" email notification.

add_action( 'woocommerce_order_status_changed', 'sending_on_hold_email_notification', 20, 4 );
function sending_on_hold_email_notification( $order_id, $old_status, $new_status, $order ){
    // Only  "On hold" order status and "Free Shipping" Shipping Method
    if ( $order->has_shipping_method('free_shipping') && $new_status == 'on-hold' ){
        // Getting all WC_emails objects
        $notifications = WC()->mailer()->get_emails();
        // Send "On hold" email notification
        $notifications['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
    } elseif ( ! $order->has_shipping_method('free_shipping') && $new_status == 'completed' ){
        // Getting all WC_emails objects
        $notifications = WC()->mailer()->get_emails();
        // Send "On hold" email notification
        $notifications['WC_Email_Customer_Completed_Order']->trigger( $order_id );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your answer. I highly appreciate it. So the first code that you have given in your answer; does it send the 'Order on Hold' email automatically if the 'Alternate' shipping method is selected by the customer? Instead of the default WooCommerce order email? – Hamza Ahmad Jun 24 '18 at 11:21
  • Thanks for your comment. I appreciate your help greatly. I want to automatically trigger the 'On-hold' email if any customer chooses the free shipping but send the normal completed order email if they choose DHL shipping. Could you please edit the code to function in the way I described? Thanks again. – Hamza Ahmad Jun 25 '18 at 04:11
  • Thanks for your answers and help. I have accepted your answer as the correct answer. – Hamza Ahmad Jun 25 '18 at 06:19