Setting Billing Address Required
-
For some reason my checkout billing fields show up as required, then once the page has loaded completely they change to optional, I think once the “Order Review” sidebar has loaded with the PayPal buttons. (only visually, errors appear if they’re not filled).
I’ve found out that with the plugin “WooCommerce PayPal Checkout Gateway” deactivated the fields stay as * required.
The plugin “WooCommerce PayPal Checkout Gateway” uses the hook “woocommerce_default_address_fields” to set all fields optional.
/** * If the cart doesn't need shipping at all, don't require the address fields * (this is unique to PPEC). This is one of two places we need to filter fields. * See also filter_billing_fields below. * * @since 1.2.1 * @since 1.5.4 Check to make sure PPEC is even enable before continuing. * @param $fields array * * @return array */ public function filter_default_address_fields( $fields ) { if ( 'yes' !== wc_gateway_ppec()->settings->enabled ) { return $fields; } if ( ! apply_filters( 'woocommerce_paypal_express_checkout_address_not_required', ! WC_Gateway_PPEC_Plugin::needs_shipping() ) ) { return $fields; } if ( method_exists( WC()->cart, 'needs_shipping' ) && ! WC()->cart->needs_shipping() && 'no' === wc_gateway_ppec()->settings->require_billing ) { // I don't understand this part, why the require_billing check, if this is just for shipping why use woocommerce_default_address_fields instead of woocommerce_shipping_fields? $not_required_fields = array( 'first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'postcode', 'country' ); foreach ( $not_required_fields as $not_required_field ) { if ( array_key_exists( $not_required_field, $fields ) ) { $fields[ $not_required_field ]['required'] = false; } } } // Regardless of shipping, PP doesn't have the county required (e.g. using Ireland without a county is acceptable) if ( array_key_exists( 'state', $fields ) ) { $fields['state']['required'] = false; } return $fields; }Here are some screenshots.
This is the checkout page when the page has just loaded.
I added the <!– DEBUG in loop $checkout->get_checkout_fields( ‘billing’ ); –> in the “woocommerce/checkout/form-billing.php” of my parent theme.
This is after it’s finished loading.
The topic ‘Setting Billing Address Required’ is closed to new replies.