skip to Main Content

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 emails/email-addresses.php despite it being a local pickup method.

My code attempt:

<?php
/**
 * Email Addresses
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-addresses.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerceTemplatesEmails
 * @version 3.9.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$text_align = is_rtl() ? 'right' : 'left';
$address    = $order->get_formatted_billing_address();
$shipping   = $order->get_formatted_shipping_address();


?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
    <tr>
        <td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
            <h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>

            <address class="address">
                <?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
                <?php if ( $order->get_billing_phone() ) : ?>
                    <br/><?php echo wc_make_phone_clickable( $order->get_billing_phone() ); ?>
                <?php endif; ?>
                <?php if ( $order->get_billing_email() ) : ?>
                    <br/><?php echo esc_html( $order->get_billing_email() ); ?>
                <?php endif; ?>
            </address>
        </td>
        <?php if ( $shipping ) : ?>
            <td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; padding:0;" valign="top" width="50%">
                <h2><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>

                <address class="address"><?php echo wp_kses_post( $shipping ); ?></address>
            </td>
        <?php endif; ?>
    </tr>
</table>

I need this to be enabled as some of our other pickup methods require it. And I am trying to have it so that if it is solely "Pick up at Rockefeller Store" then it should hide the shipping address in new order WooCommerce email.

How would I filter this based on its text label as shown in the picture?

enter image description here

3

Answers


  1. Chosen as BEST ANSWER
    $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


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

    Login or Signup to reply.
  3. Looking for a way to hide the address and other fields for local pickup method I see this reply https://wpsimplehacks.com/how-to-hide-woocommerce-checkout-fields-when-local-pickup-is-selected/

    This is the code and works for me:

    // Hide Local Pickup shipping method
    add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' );
    function hide_local_pickup_method( $fields_pickup ) {
        // change below for the method
        $shipping_method_pickup ='local_pickup:4';
        // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
        $hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city', 'billing_state');
     
        $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping_pickup = $chosen_methods_pickup[0];
     
        foreach($hide_fields_pickup as $field_pickup ) {
            if ($chosen_shipping_pickup == $shipping_method_pickup) {
                $fields_pickup['billing'][$field_pickup]['required'] = false;
                $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
            }
            $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
        }
        return $fields_pickup;
    }
    // Local Pickup - hide fields
    add_action( 'wp_head', 'local_pickup_fields', 999 );
    function local_pickup_fields() {
        if (is_checkout()) :
        ?>
        <style>
            .hide_pickup {display: none!important;}
        </style>
        <script>
            jQuery( function( $ ) {
                if ( typeof woocommerce_params === 'undefined' ) {
                    return false;
                }
                $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                    // change local_pickup:4 accordingly
                $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'local_pickup:4');
                });
            });
        </script>
        <?php
        endif;
    }
    

    If you need to hide the "send to different address" on checkout page try this:

    add_action( 'woocommerce_after_checkout_form', 'hide_ship_to_section' );
      
    function hide_ship_to_section( $available_gateways ) {
        
       $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
       $chosen_shipping = $chosen_methods[0];
       if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
       ?>
          <script type="text/javascript">
             jQuery('#customer_details .col-2').fadeOut();
          </script>
       <?php  
       } 
       ?>
          <script type="text/javascript">
             jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
                var val = jQuery( this ).val();
                if (val.match("^local_pickup")) {
                         jQuery('#customer_details .col-2').fadeOut();
                   } else {
                   jQuery('#customer_details .col-2').fadeIn();
                }
             });
          </script>
       <?php
      
    }
    

    All credit is for Janek T, couldn´t find his profile at stackoverflow

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search