-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix required fields breaking Google Pay and Apple pay #2886
Conversation
…ields" This reverts commit 86898ea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for all your guidance through testing this!
I had only one issue that does not seem related to your change. When checking out using the Cart Block directly, Google form keeps loading for like 2 minutes before stopping and disabling the G Pay button. I will do more tests here.
Can someone clarify how that error is generated? I am using the WooCommerce Checkout Fields extension to add additional required fields and they seemed ignored. |
@therealgilles The validation for the default checkout fields is done in the validate_posted_data function and the error notice is generated here. The validation is done during process_checkout. You can add your custom validation by making use of any available hooks, e.g woocommerce_checkout_process action etc. |
@Mayisha Thank you, that's very helpful. One thing I've noticed is that I have a required checkbox and using Apple Pay or Google Pay automatically considers the box checked. It does not show an error if left unchecked and does not tell the user about it. I don't think this has to do with my setup and it seems either an oversight or an odd on-purpose behavior. If on-purpose, it should be documented. PS: Your 2nd link is the same as the first one, probably a copy/paste error? UPDATE: I checked the [my_checkbox] => Array
(
[type] => checkbox
[label] => I agree to this
[options] => Array
(
)
[placeholder] =>
[priority] => 50
[enabled] => 1
[validate] => Array
(
[0] => required
)
[required] => 1
[custom] => 1
[display_options] => Array
(
[0] => emails
[1] => view_order
)
[class] => Array
(
[0] => form-row-wide
)
) UPDATE1: Looking at the ...
[payment_method] => stripe
[terms] => 1
[payment_request_type] => google_pay
[shipping_email] => ...
[my_checkbox] => 1
[g-recaptcha-response] => null |
The JS code below makes the classic mistake of using See: https://stackoverflow.com/questions/5621324/jquery-val-and-checkboxes getRequiredFieldDataFromCheckoutForm: function( data ) {
const requiredfields = $( 'form.checkout' ).find( '.validate-required' );
if ( requiredfields.length ) {
requiredfields.each( function() {
const field = $( this ).find( ':input' );
const value = field.val(); // <-- HERE
const name = field.attr( 'name' );
if ( value && name ) {
if ( ! data[ name ] ) {
data[ name ] = value;
}
// if shipping same as billing is selected, copy the billing field to shipping field.
const shipToDiffAddress = $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ); Here is the code from WooCommerce in comparison: if ( validate_required ) {
if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
} else if ( $this.val() === '' ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
}
} Please fix :) |
It also appears that the I'm going to add one more thing. The UX would be way better if the (required) fields were validated before the Apple/Google Pay dialog opens up. This could be fairly easily done by capturing the button click. |
Fixes #2343 #2169
If there is any required field on the checkout page, for example
Company
field, user can not purchase with Google/Apple pay. The reason of this issue is, when we create the order during payment, the validation fails due to the required field. Because we map the order data from the Google Pay response which does not include the required field (company).Changes proposed in this Pull Request:
WC()->checkout()->get_checkout_fields()
. Similarly when the shop is using shortcode checkout (and has required field) we hide the button from product page as well.get_checkout_fields
function to utilize for the blocks. However, the js error mentioned in Required fields breaking Apple Pay #2169 does not happen here and properly shows the error message on top of the page.Testing instructions
Shortcode
Company
name field required.company
field. You should see an error message on the page.company
field. Try again to pay with Google/Apple Pay. This time the purchase should be successful.Ship to a different address
checked/unchecked and verify the order data.Block
Company
name field required from the right side panel. Similarly, make theCompany
field required for the billing address.company
field. You should see an error message on the page.company
field. Try again to pay with Google/Apple Pay. This time the purchase should be successful.Use same address for billing
checked/unchecked and verify the order data.