• Hello, I’ve added a custom checkbox with special Terms and conditions. The validation of this required checkbox works fine for wire transfers but, unfortunately, not for the PayPal button. What hook/action should I use, please?

    The checkbox is inserted via this action to Cart:
    add_action( 'woocommerce_checkout_before_terms_and_conditions', 'add_checkout_checkbox' );

    The validation is right now happening like this

    add_action( 'woocommerce_after_checkout_validation', 'add_checkout_checkbox_warning' );
    /**
    * Alert if checkbox not checked
    */
    function add_checkout_checkbox_warning() {
    if ( ! (int) isset( $_POST['checkout_checkbox'] ) ) {
    wc_add_notice( __( 'Please accept the terms and condition.' ), 'error' );
    }
    }


    I think the problem is with calling the checkout validation, because warning is ignored when the PayPal button is clicked. For WireTransfer it works.

    The plugin option Validate Checkout Fields in Settings in set up to TRUE.

    Thank you for your help

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Clayton R

    (@mrclayton)

    Hi @kosak

    The validation of this required checkbox works fine for wire transfers but, unfortunately, not for the PayPal button. 

    That’s because the wire transfers gateway just relies on the core WooCommerce validations, it doesn’t do anything special so comparing the two plugins doesn’t make sense. If you were using the Use Place Order Button option in the PayPal plugin, then you would observer that same validation being triggered.

    Since it sounds like you’re using the PayPal smartbutton, you should use action wc_ppcp_checkout_validation. If you were adding your custom field using WooCommerce core filters like woocommerce_checkout_fields the PayPal plugin would know to include that in the validation. But since you aren’t you need to tell the plugin what to validate for your custom field.

    Example:

    add_action('wc_ppcp_checkout_validation', function($validator, $request){
        if ( ! (int) isset( $request['checkout_checkbox'] ) ) {
            $validator->add_error( __( 'Please accept the terms and condition.' ));
        }
    }, 10, 2);

    Kind Regards

    Thread Starter david.nemec

    (@kosak)

    Hello,

    thank you very much for your help and hints. I’ve just tried it but no success. PayPal button is still ignoring the unchecked checkbox and opening the modal for payment. But when I’m not inserting requeired field (like a ZIP code or address), the validation works – modal is opened and than close immediately because some fields are not filled.

    I’m using this type of PayPal button – https://i.imgur.com/vbRT0vl.png

    And this is the whole code I’m using it for (also with your part at the end) – https://codefile.io/f/emBZ91A0KJ

    Thank you very much.
    All the best, David

    Plugin Author Clayton R

    (@mrclayton)

    @kosak I believe the formatting in your custom code is incorrect. Notice that your editor highlights strings in yellow but the filter wc_ppcp_checkout_validation is shown in red.

    Thread Starter david.nemec

    (@kosak)

    Thank you very much. You were right 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom checkbox – add hook for validation’ is closed to new replies.