• Resolved vincentra

    (@vincentra)


    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.

    View post on imgur.com

    This is after it’s finished loading.

    View post on imgur.com

    • This topic was modified 6 years, 2 months ago by vincentra.
Viewing 1 replies (of 1 total)
  • Plugin Contributor slash1andy

    (@slash1andy)

    Automattic Happiness Engineer

    Hey there!

    PayPal Checkout needs for the address to not be required for it to work, using the code you are seeing above. Changing how the plugin works will likely result in it no longer working. If you want to have it still be required, you would likely want to use the PayPal Standard found in WooCommerce core, not PayPal Checkout.

    Hopefully that helps! Have a great one!

Viewing 1 replies (of 1 total)

The topic ‘Setting Billing Address Required’ is closed to new replies.