carlaa123
carlaa123

Reputation: 65

Add message based on shipping method ID in WooCommerce customer completed order email notification

I want to add a message based on the shipping method ID in the WooCommerce customer completed order email notification.

I am using following code:

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 "completed" email notification and "Local Pickup" Shipping Method
    if ( 'customer_completed_order' == $email->id && $order->has_shipping_method('local_pickup') ){
        $order_id = $order->get_id(); // The Order ID

        // Your message output
        echo "<h2>TEST</h2>
        <p>Shipping info test</p>";
    }
}

This works but I have 2 different local pickup

Is to possible to set this to only local_pickup:10?

Upvotes: 3

Views: 1006

Answers (2)

Tom Mills
Tom Mills

Reputation: 1

Added option for orders that do not have shipping

function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email
if ( $email->id == 'customer_completed_order' ) {
    // Get the shipping method ID
    $shipping_items      = $order->get_items( 'shipping' );
    $shipping_item       = reset( $shipping_items );

    if ( $shipping_item instanceof WC_Order_Item_Shipping ) {
        // Has a shipping method
        $shipping_method_id  = $shipping_item->get_method_id() . ':';
        $shipping_method_id .= $shipping_item->get_instance_id();

        // Compare
        if ( $shipping_method_id == 'flat_rate:1' ) {
            echo 'Thank you for your order 1';
        } elseif ( $shipping_method_id == 'flat_rate:9' ) {
            echo 'Thank you for your order 2';
        } else {
            echo 'DEBUG INFORMATION = ' . $shipping_method_id;
        }
    } else {
        // Doesn't have a shipping method
        echo 'Thank you for your order NO SHIPPING';
    }
  }
}

add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );

Upvotes: 0

7uc1f3r
7uc1f3r

Reputation: 29614

To get the shipping method ID, you can use $shipping_item->get_method_id() + $shipping_item->get_instance_id() and concatenate the string

So you get:

function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    // Target specific email
    if ( $email->id == 'customer_completed_order' ) {
        // Get the shipping method ID
        $shipping_items      = $order->get_items( 'shipping' );
        $shipping_item       = reset( $shipping_items );
        $shipping_method_id  = $shipping_item->get_method_id() . ':';
        $shipping_method_id .= $shipping_item->get_instance_id();
         
        // Compare
        if ( $shipping_method_id == 'local_pickup:1' ) {
            echo 'test 1';
        } elseif ( $shipping_method_id == 'local_pickup:10' ) {
            echo 'test 2';
        } elseif ( $shipping_method_id == 'local_pickup:11' ) {
            echo 'test 3';
        } else {
            echo 'DEBUG INFORMATION = ' . $shipping_method_id;
        }
     }
}
add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );

Upvotes: 4

Related Questions