最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove checkout fields with Woocommerce depending on one of several shipping methods

programmeradmin2浏览0评论

I've added the following code to remove some checkout fields in case a particular shipping method is chosen and also make it work when you change the shipping method on the checkout page.

add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
    // change below for the method
    $shipping_method ='local_pickup:1'; // Here I want to specify an array of shipping methods
    // change below for the list of fields
    $hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping = $chosen_methods[0];

    foreach($hide_fields as $field ) {
        if ($chosen_shipping == $shipping_method) {
            $fields['billing'][$field]['required'] = false;
            $fields['billing'][$field]['class'][] = 'hide';
        }
        $fields['billing'][$field]['class'][] = 'billing-dynamic';
    }

    return $fields;
}

add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
    if (is_checkout()) :
    ?>
    <style>
        .hide {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change local_pickup:1 accordingly 
                $('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
            });
        });
    </script>
    <?php
    endif;
}

I was wondering how to make this work for an array of shipping methods. Suppose I want to make this work for flat_rate:2 as well, how could I elegantly loop the array to hide the fields an all shipping method of the array.

I've added the following code to remove some checkout fields in case a particular shipping method is chosen and also make it work when you change the shipping method on the checkout page.

add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
    // change below for the method
    $shipping_method ='local_pickup:1'; // Here I want to specify an array of shipping methods
    // change below for the list of fields
    $hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping = $chosen_methods[0];

    foreach($hide_fields as $field ) {
        if ($chosen_shipping == $shipping_method) {
            $fields['billing'][$field]['required'] = false;
            $fields['billing'][$field]['class'][] = 'hide';
        }
        $fields['billing'][$field]['class'][] = 'billing-dynamic';
    }

    return $fields;
}

add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
    if (is_checkout()) :
    ?>
    <style>
        .hide {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change local_pickup:1 accordingly 
                $('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
            });
        });
    </script>
    <?php
    endif;
}

I was wondering how to make this work for an array of shipping methods. Suppose I want to make this work for flat_rate:2 as well, how could I elegantly loop the array to hide the fields an all shipping method of the array.

Share Improve this question asked Jun 6, 2020 at 12:20 BarrieOBarrieO 992 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Sorry Not really a great answer. However, I haven't got enough Reputation points to add a comment yet :(

I was just reading this site, and I saw the following snippet that could be useful for what you are trying to do.

(this is not my code I use this page for a lot of great ideas & tips)

/**
 * @snippet       Simplify Checkout if Only Virtual Products
 * @how-to        Get CustomizeWoo FREE
 * @sourcecode    https://businessbloomer/?p=78351
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer/bloomer-armada/
 */

add_filter( 'woocommerce_checkout_fields' , 'bbloomer_simplify_checkout_virtual' );

function bbloomer_simplify_checkout_virtual( $fields ) {

   $only_virtual = true;

   foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      // Check if there are non-virtual products
      if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;   
   }

    if( $only_virtual ) {
       unset($fields['billing']['billing_company']);
       unset($fields['billing']['billing_address_1']);
       unset($fields['billing']['billing_address_2']);
       unset($fields['billing']['billing_city']);
       unset($fields['billing']['billing_postcode']);
       unset($fields['billing']['billing_country']);
       unset($fields['billing']['billing_state']);
       unset($fields['billing']['billing_phone']);
       add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
     }

     return $fields;
}

I hope you find it useful :)

I came up with the following code for my problem:

/* HIDE "POSTKANTOOR" FIELD WHEN NON-POSTKANTOOR SHIPPING OPTION IS SELECTED */
/* --- */
add_filter( 'woocommerce_checkout_fields', 'remove_billing_checkout_fields' );
function remove_billing_checkout_fields( $fields ) {
    // Postkantoor shipping methods
    $shipping_method = array( 'flat_rate:6','flat_rate:7','flat_rate:8' ); 
    // Fields to hide if not postkantoor shipping method
    $hide_fields = array( 'postkantoor' );

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping = $chosen_methods[0];

    foreach( $hide_fields as $field ) {
        if ( ! in_array ($chosen_shipping , $shipping_method) ) {
            $fields['billing'][$field]['required'] = false;
            $fields['billing'][$field]['class'][] = 'hide';
        }
        $fields['billing'][$field]['class'][] = 'billing-dynamic';
    }

    return $fields;
}

add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
    if (is_checkout()) :
    ?>
    <style>
        .hide {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            // Postkantoor shipping methods
            var show = ['flat_rate:6','flat_rate:7','flat_rate:8'];

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
              // console.log($.inArray($(this).val(), show));
              if ($.inArray($(this).val(), show) > -1) {    // >-1 if found in array 
                $('.billing-dynamic').removeClass('hide');
                // console.log('show');
              } else {
                $('.billing-dynamic').addClass('hide');
                // console.log('hide');
              }
            });

        });
    </script>
    <?php
    endif;
}
发布评论

评论列表(0)

  1. 暂无评论