0

I have two flat rates (flat rate:1 and flat rate:2) I would like to hide the "Shipping Address" in WooCommerce Thank you page when "Flat rate:2" shipping method is chosen.

Trying to use something based on WooCommerce - Hide shipping address when local pickup is chosen on thankyou page, but I'm stuck.

Can anyone give a some tips how to achieve this?

Woocommerce 5.1.0

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Jeronimo
  • 13
  • 1

1 Answers1

1

Updated

You can use this simple code snippet, to hide shipping address from orders based on specific defined shipping methods:

add_filter( 'woocommerce_order_needs_shipping_address', 'filter_order_needs_shipping_address', 10,  );
function filter_order_needs_shipping_address( $needs_address, $hide, $order ) {
    // Here set your shipping method instance Ids
    $targeted_instances_ids = array( 1, 2 ); 
    
    // Loop through "shipping" order items
    foreach ( $order->get_items('shipping') as $item ) {
        if( in_array( $item->get_instance_id(), $targeted_instances_ids ) ) {
            return false;
        }
    }
    return $needs_address;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Note: The shipping instance ID is the number after : in a shipping method rates ID.
So for example for flat rate:2, the shipping instance Id is 2.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • First of all thanks for quick respond. I'm trying to add code but I received fatal error on line 9: syntax error, unexpected 'return' (T_RETURN). As I understand correctly if I set $ targeted_instances_ids = array (2); the shipping adress for flat rate 2 should hide on order page. – Jeronimo Mar 10 '21 at 14:25
  • Thanks for your time. After I added propper code shipping address does not show up in flatrate 1 and flat rate 2. Only billing address is shown. [link](https://1drv.ms/u/s!AvSMMyCQWuCEtT9XMN3OkguL264A?e=qAsQQT) – Jeronimo Mar 10 '21 at 15:41
  • Ok thanks for your help I will investigate by me side what I can do. Maybe Im missing something earlier. – Jeronimo Mar 10 '21 at 15:53
  • Finally I have fixed the reason why your code did not work for me. I had to change the values to 'add_filter( 'woocommerce_order_needs_shipping_address', 'filter_order_needs_shipping_address', 10,2 ); function filter_order_needs_shipping_address( $needs_address, $hide, $order ) {' – Jeronimo Mar 14 '21 at 11:49
  • @Jeronimo Oups yes sorry!… Forgot to rename the function too… Updated – LoicTheAztec Mar 14 '21 at 11:50