Home > Mobile >  Hide shipping address in WooCommerce new order email notification depending on shipping method id
Hide shipping address in WooCommerce new order email notification depending on shipping method id

Time:09-24

I want to hide the shipping address if the shipping label is called "Pick up at Rockefeller Store" (but to show for other pickup methods).

There are too many ids such as "local_pickup:3" for me to filter through. I enabled the shipping address to be shown in enter image description here

CodePudding user response:

There is no need to edit template files as it is possible via hooks. This answer consists of 2 parts:

1) The first part only needs to be executed once and serves as debug information. The following text will appear in the new order mail

DEBUG INFORMATION, shipping method id = THE_SHIPPING_METHOD_ID

Part 1:

// DEBUG information
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {           
    // Only for 'new order'
    if ( $email->id == 'new_order' ) {
        // Get shipping methods
        foreach ( $order->get_shipping_methods() as $shipping_method ) {
            // Get method ID
            $shipping_method_id = $shipping_method->get_method_id();

            // Output
            echo '<p>DEBUG INFORMATION, shipping method id = ' . $shipping_method_id . '</p>';
        }   
    }
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );

NOTE: After the correct information is known, you should no longer use part 1


2) Then you can use the 2nd part of my answer, where you need to replace REPLACE_THIS_WITH_THE_SHIPPING_METHOD_ID with THE_SHIPPING_METHOD_ID, that information comes from the first part of my answer

Part 2:

// Setting the email_is as a global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {           
    $GLOBALS['email_id_str'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );

function filter_woocommerce_order_hide_shipping_address( $shipping_method_id, $order ) {
    // Getting the email ID global variable
    $refNameGlobalsVar = $GLOBALS;
    $email_id = isset( $refNameGlobalsVar['email_id_str'] ) ? $refNameGlobalsVar['email_id_str'] : '';

    // Only for 'new order'
    if ( $email_id == 'new_order' ) {
        // Replace the 2nd part in the array with the 'shipping_method_id' from the DEBUG INFORMATION
        $shipping_method_id = array( 'local_pickup', 'REPLACE_THIS_WITH_THE_SHIPPING_METHOD_ID' );
    } else {
        // Default
        $shipping_method_id = array( 'local_pickup' );  
    }
    
    return $shipping_method_id;
}
add_filter( 'woocommerce_order_hide_shipping_address', 'filter_woocommerce_order_hide_shipping_address', 10, 2 );

NOTE: Since the email id is not known in the 2nd part of my answer, a workaround is used for this, see: https://stackoverflow.com/a/43574008/11987538

CodePudding user response:

$shipping_local_pickup = false;
        if ( $items_totals = $order->get_order_item_totals() ) {
            foreach ( $items_totals as $items_total ) {
                if ( $items_total['value'] == 'Pick Up at Rockefeller Store' && !$shipping_local_pickup ) $shipping_local_pickup = true;
            }
        }   

Adding this to my template worked then setting line 48 to !$shipping_local_pickup

This allows me to choose the phrase "Pick Up at Rockefeller Store" used in the table and not display the shipping address it if it is present.

Answer was taken and edited from Hide shipping address on local pickup in WooCommerce email notifications

  • Related