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() action, but I can’t seem to figure out what that problem is. Can anyone else?

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' );
            }

}

Read more here: Woocommerce – Second T&C checkbox doesn’t prevent checkout when left unchecked


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: