• Resolved marinow1991

    (@marinow1991)


    Hello ?? I’m trying to hide some fields in the checkout, based on the chosen delivery method.

    This code that I have given works for me, but I manage to hide the fields with only one method and not with the other. How could I add another method to the code?

    // 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:5′;
    // 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_adres3’, ‘billing_country’, ‘billing_postcode’, ‘billing_address_2’ , ‘billing_city’ );

    $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:5’);
    });
    });
    </script>
    <?php
    endif;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Have you considered using a plugin like this (https://www.ads-software.com/plugins/flexible-checkout-fields/)?

    If you are just looking for help with the customization of the code. I can recommend the following places for more development-oriented questions:

    WooCommerce Slack Community: https://woocommerce.com/community-slack/WooCommerce Community on Facebook: https://www.facebook.com/groups/advanced.woocommerce/

    Hi @marinow1991

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. If you have further questions, please feel free to open a new topic.

    Hello everyone :), as marinow1991, I am trying to hide some fields in the checkout process, depending on the delivery method chosen.

    This code works for me, but the value of the element (shipping_method_pickup), is not constant, apparently it can be presented in instances. Example:

    1- $ shipping_method_pickup = ’local_pickup ′; or
    2- $ shipping_method_pickup = ’local_pickup:1 ′; or
    3- $ shipping_method_pickup = ’local_pickup:2 ′; or
    4- $ shipping_method_pickup = ’local_pickup:x ′; and so on,

    Where x is the instance number. So this script doesn’t work for all cases, only for the one we assigned when defining it. You know how I could modify it to work for this general rule, or the values ??that start with $ shipping_method_pickup = ’local_pickup ***’?

    My page is https://paisesmarket.com/inicio, in case you want to see it.
    I appreciate your help!! Thanks!

    Since this requires custom coding, I’m leaving this thread open for a bit to see if anyone can chime in to help you out.

    For additional assistance on this topic, we recommend getting in touch with one of the customization experts listed on the WooCommerce Customizations Page.

    And as @maykato recommends, if you visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack, you could find help from the community of open-source developers for WooCommerce that hangs in there.

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide checkout fields based on the delivery method’ is closed to new replies.