Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Woocommerce - Second T&C checkbox doesn't prevent checkout when left unchecked

I have a special checkbox I need placed only if the customer purchases one of two specific products. Code below looks for these product_ids, and if found, displays the additional Terms and Conditions box next to the regular T&C checkbox. While this part is working, the checkout still completes even if the box is left unchecked, so I'm guessing the problem is within the aym_not_approved_amb_terms() function, but I can't seem to figure out what that problem is.

add_action( 'woocommerce_after_checkout_form', 'aym_add_ambassador_terms_box' );
function aym_add_ambassador_terms_box() {
    // set product IDs:
    $product_ids = array( 17558, 17563 );
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( in_array( $item->id, $product_ids ) )
            $bool = true;
    }
    // If product IDs detected
    if ($bool)
        // add ambassador terms checkbox
            add_action('woocommerce_review_order_after_order_total', 'aym_add_checkout_tickbox' );

            function aym_add_checkout_tickbox() {
        echo '<script>console.log("checkbox")</script>';

                echo '<p class="form-row-wide terms">';
                echo '<input type="checkbox" class="input-checkbox" name="amb-terms-check" id="amb-terms-check" />';
                echo '<label for="amb-terms-check" class="checkbox">I accept Ambassador terms and Conditions</label>';
                echo '</p>';

            }

            // Show notice if customer does not tick

            add_action('woocommerce_checkout_process', 'aym_not_approved_amb_terms');

            function aym_not_approved_amb_terms() {
                if ( ! $_POST['amb-terms-check'] )
                    wc_add_notice( __( 'Please agree to the Ambassador Terms and Conditions' ), 'error' );
            }

}

Expected behavior: if the box is left unchecked, page scrolls back up to the error message area and displays the message 'Please agree to the Ambassador Terms and Conditions'. Maybe I'm not using the correct hook for the aym_not_approved_amb_terms function...?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Because your validation is inside a conditional that only loads once the form is loaded, your validation callback doesn't exist when the form is actually being validated.

So what we're going to do is unwrap the add_action() calls from inside the conditional check and put the conditional check inside the callbacks. Now, this kinda means we have to check the cart twice, and I think there must be a more efficient way to do this but I thought about adding a flag in the cart when the item is added, however the cart classes magic __get() method will now prevent $cart->my_flag from returning any useful values. SO, we'll just check the cart twice for now.

Since we will check the cart twice, I extrapolated your conditional out into a function that we can reuse.

function aym_is_item_in_cart() {

    $matching_product_ids = array( 17558, 17563 );

    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( in_array( $cart_item['product_id'], $matching_product_ids ) ) {
            $bool = true;
        }
    }
    return $bool;
}

Then we add the checkbox. In WooCommerce 3.0, I found an issue where the field was repeating when adding to the woocommerce_review_order_after_order_total hook, so I've added it to the woocommerce_checkout_after_customer_details instead. note how I'm always adding the callback to this hook, but the checkbox is inside a conditional. Also note that I'm using the built-in woocommerce_form_field() helper function to create the form field instead of adding it via javascript.

// add ambassador terms checkbox
add_action( 'woocommerce_checkout_order_review', 'aym_maybe_add_ambassador_terms_box', 5 );

function aym_maybe_add_ambassador_terms_box() {

    // If product IDs detected
    if ( aym_is_item_in_cart() ) {

    $checkout = WC()->checkout();

        $field = array(
            'type'              => 'checkbox',
            'label'             => __( 'I accept Ambassador terms and Conditions', 'your-plugin' ),
            'required'          => true,
            'class'             => array( 'form-row-wide', 'terms'),
            'validate'          => array( 'terms' ),
        );

        woocommerce_form_field( 'amb-terms-check', $field, $checkout->get_value( 'amb-terms-check' ) );

    }

}

Lastly, some validation. We need to check if we need the checkbox and then check if it's checked, otherwise this will block checkouts that don't have the triggering products in the checkout.

add_action( 'woocommerce_before_checkout_process', 'aym_not_approved_amb_terms' );

function aym_not_approved_amb_terms() {

    if ( aym_is_item_in_cart() && ! isset( $_POST['amb-terms-check'] ) ) {
        wc_add_notice( __( 'Please agree to the Ambassador Terms and Conditions' ), 'error' );
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!