Home > database >  Hide Woocommerce "Ship to different address" if custom radio option is selected
Hide Woocommerce "Ship to different address" if custom radio option is selected

Time:10-05

I have a custom radio selection with "Yes" and "No answers" on my Woocommerce checkout page. I use this code here below to add this selection. Is there a way to make "Ship to different address" selection area to disappear if "yes" is selected? Thanks

add_action( 'woocommerce_before_checkout_billing_form', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () { ?>
    <h3>Do you have a PO box?<sup>*</sup></h3>
    <span><input type="radio" name="delivery_option" value="Yes" required /> Yes </span>
    <span><input type="radio" name="delivery_option" value="No" />No</span>
  <?php 
}

CodePudding user response:

You can try this one.

jQuery('#ship-to-different-address').hide();

Place this code in your functions.php at the bottom

function add_custom_scripts(){ ?>
    <script type="text/javascript">
        (function($){
            $( document ).ready(function() {
                $('input[name=delivery_option]').change(function(){
                    var po_box_val = $('input[name="delivery_option"]:checked').val();
                    if ( po_box_val === "Yes") {
                        jQuery('#ship-to-different-address').hide();
                    }else if (po_box_val === "No"){
                        jQuery('#ship-to-different-address').show();
                    }
                });
            });
        })(jQuery);
    </script>
<?php }
add_action( 'wp_footer', 'add_custom_scripts', 10, 1 );
  • Related