2
votes

I am trying to disable the Billing fields in the checkout page when user selects the paypal payment method in WooCommerce wordpress theme.

Once the user complete the item selectionand comes to checkout page, if the user selects the paypal payment then the checkout form should not validate the billing fields if any other payment methos is selected then it should work as normal.

I searched alot but not able to find the clue. Please some one help to me on this.

Thanks in Advance.

2

2 Answers

0
votes
/* if you want the title of the payment method */
add_action( 'woocommerce_review_order_before_payment', 'ts_refresh_payment_method' );
function ts_refresh_payment_method(){
$chosen_payment_method = WC()->session->get('chosen_payment_method');  //Get the selected payment method

    // jQuery
    ?>
    <script type="text/javascript">
        (function($){
var cpm = '<?php echo $chosen_payment_method; ?>';
if(cpm == "paypal"){
$(".woocommerce-billing-fields").hide();
}else{
$(".woocommerce-billing-fields").show();
}
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
//alert(this.value);
if(this.value == "paypal"){
$(".woocommerce-billing-fields").hide();
}else{
$(".woocommerce-billing-fields").show();
}

                $('body').trigger('update_checkout');
            });
alert($('body').trigger('update_checkout'));
        })(jQuery);
    </script>
    <?php
}

/*  */
add_filter('woocommerce_billing_fields','wpb_custom_billing_fields');
function wpb_custom_billing_fields( $fields = array() ) {
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if($chosen_payment_method == "paypal"){
$fields['billing_first_name']['required']= false;
$fields['billing_last_name']['required']= false;
$fields['billing_company']['required']  = false;
$fields['billing_email']['required']   = false;
$fields['billing_address_1']['required']= false;
$fields['billing_address_2']['required']= false;
$fields['billing_state']['required']   = false;
$fields['billing_city']['required']     = false;
$fields['billing_phone']['required']    = false;
$fields['billing_postcode']['required'] = false;
$fields['billing_country']['required']  = false;
}
return $fields;
}
-2
votes

Take a look at PayPal for WooCommerce. It's free and it will allow you to enable PayPal Express Checkout instead of PayPal Standard, which flows a lot more nicely for people paying with PayPal.