简体   繁体   中英

Send Order On Hold Email for a specific shipping method in Woocommerce

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.

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.

在此输入图像描述


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM