angelleye / paypal-woocommerce

PayPal payment gateway extension for WooCommerce.
GNU General Public License v3.0
125 stars 78 forks source link

custom validation of (conditionally required) field is being skipped by Paypal Express Checkout #1405

Closed gbruzzo closed 4 years ago

gbruzzo commented 4 years ago

Hello there

We are experiencing the following issue:

We have implemented test code in functions.php which will force a checkout field to become 'required' (in our case the $billing_phone field) when a particular shipping method is selected (in our case the local pickup method).

We include the code just below, but please scroll down to the bottom where we describe the ISSUE we are facing.

this code is a version of LINK

add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {

    $local_pickup = 'local_pickup:6';

    $required_text = esc_attr__( 'required', 'woocommerce' );
    $required_html = '<abbr class="required" title="' . $required_text . '">*</abbr>';
    ?>
    <script>
        jQuery(function($){
            var ism = 'input[name^="shipping_method"]',         ismc = ism+':checked',
                rq = '-required',       vr = 'validate'+rq,     w = 'woocommerce',      wv = w+'-validated',
                iv = '-invalid',        fi = '-field',          wir = w+iv+' '+w+iv+rq+fi,
                b = '#billing_',        f = '_field',
                a1 = 'phone', 
                b1 = b+a1+f,
                localPickup = '<?php echo $local_pickup; ?>';

            function makereq( action='req', selector='' ){
                if( action == 'req' ) {
                        $(selector).addClass(vr);
                        $(selector).removeClass(wv);
                        $(selector).removeClass(wir);
                            $(selector+' label > .optional').remove();
                            $(selector+' label').append('<?php echo $required_html; ?>');
                        }
                else       {
                        $(selector).removeClass(vr);
                        $(selector).removeClass(wv);
                        $(selector).removeClass(wir);
                        $(selector+' label > .required').remove();
                        $(selector+' label').append('<span class="optional">(optional)</span>');
          }}

            setTimeout(function(){
                if( $(ismc).val() == localPickup ) // Chosen "Pick point" (Hiding "Delivery")
                {
                    makereq('req',b1 ); // Country
                }
                else
                {
                    makereq('opt',b1);
                }
            }, 100);

            $( 'form.checkout' ).on( 'change', ism, function() {
                if( $(ismc).val() == localPickup )
                {
                    makereq('req',b1);
                }
                else
                {
                    makereq('opt',b1);
                }
            });

        });
    </script>
    <?php
}

add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {

    $local_pickup = 'local_pickup:6';
    $chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' )[0];
    $billing_phone          = '<strong> ' . __('phone', 'woocommerce') . ' ';
    $end_text               = '</strong> '. __('is a required field.', 'woocommerce');

    if( $chosen_shipping_method == $local_pickup ) {
        if( empty($_POST['billing_phone']) )
            wc_add_notice( $billing . $billing_phone . $end_text, 'error' );
    }
}

THE ISSUE:

selecting local pickup and leaving the phone number field blank results in the correct behaviour when using a simple payment method like Check Payments or Direct Bank Transfer or Cash On Delivery or even Paypal Standard' : the checkout process is interrupted and the error notice 'phone is a required field.' appears as per our code.

BUT: selecting Paypal Express Checkout results in the validation step being ignored (the paypal popup appears as if the phone number was there, which it isn't). Proceeding with the checkout results in a 'page not found' upon return to the Checkout /Review page

If we also leave another required field (say Last name) blank, then PayPal Express Checkout will also interrupt the checkout process and send out 2 error messages, including ours.

How can we ensure that PayPal Express Checkout becomes aware of the conditional required field?

We are not sure whether this is an issue with your code, but we certainly noticed a divergence with other payment methods.

Thanks in advance

Giacomo Bruzzo

gbruzzo commented 4 years ago

Hello @angelleye

might you be able to assist on the matter above?

It would be very helpful if you could point out where in the code you define the condition for interrupting the checkout because the billing or shipping form are incomplete (on the Checkout Page, when the smart button is sitting at the bottom of the page).

Cheers

Giacomo Bruzzo

gbruzzo commented 4 years ago

Hello there

touching base / bumping to see if anyone could comment on the issue above.

We need to implement that conditional logic (make a field compulsory if a given shipping method is chosen), but are unable to make Checkout Express recognise this field as compulsory / required.

Standard payment methods (including PayPal standard) are able to recognise this and stop the checkout process if the field has not been filled when compulsory.

Thank you in advance

Giacomo Bruzzo

angelleye commented 4 years ago

@gbruzzo So sorry to have left you hanging here. It seems our GitHub notices were getting left out of our regular Help Desk routine there for a little while. Sorry about that!

Are you still looking for some help with this? For future reference, it would be great if you submit stuff like this to our help desk at https://angelleye.com/support. I'm going to fix our notices from GitHub so they go there too in the future, but for now it would be best to start there.

Please go ahead and create a ticket there if you're still looking for help, and reference this one. That would be a big help and we won't lose track of you that way. Thanks!

gbruzzo commented 4 years ago

I will do so, thank you!!

On Mon, 20 Apr 2020 at 06:56, Drew Angell notifications@github.com wrote:

@gbruzzo https://github.com/gbruzzo So sorry to have left you hanging here. It seems our GitHub notices were getting left out of our regular Help Desk routine there for a little while. Sorry about that!

Are you still looking for some help with this? For future reference, it would be great if you submit stuff like this to our help desk at https://angelleye.com/support. I'm going to fix our notices from GitHub so they go there too in the future, but for now it would be best to start there.

Please go ahead and create a ticket there if you're still looking for help, and reference this one. That would be a big help and we won't lose track of you that way. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angelleye/paypal-woocommerce/issues/1405#issuecomment-616327727, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEAFD6RSATAJLYSNOKQ664DRNPP2LANCNFSM4J7AZ5KA .