Home > front end >  Add message based on shipping method ID in WooCommerce customer completed order email notification
Add message based on shipping method ID in WooCommerce customer completed order email notification

Time:11-11

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

  • local_pickup:10
  • local_pickup:11

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

CodePudding user response:

To get the shipping method ID, you can use a $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 {
            'DEBUG INFORMATION = ' . $shipping_method_id;
        }
     }
}
add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );
  • Related