Search code examples
phpwordpresswoocommerceordersshipping-method

Hide shipping address on WooCommerce orders based on specific shipping methods


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


Solution

  • 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.