Require Accept Terms for WooCommerce “Add to Cart” Product Checkout

Learn how to require customers to accept terms before adding specific WooCommerce products to the cart by using a simple function or snippet.

By β™Ÿ Maggew.com β€” Updated   57 Views
8b1 319

Just add the snippet and specify product ID’s in the two spots below

// Add Terms Checkbox on specific product pages
add_action( 'woocommerce_before_add_to_cart_button', 'add_accept_terms_checkbox' );
function add_accept_terms_checkbox() {
    // Define the product IDs that need the checkbox
    $specific_product_ids = array( 123, 456 ); // Replace with your product IDs

    if ( is_product() && in_array( get_the_ID(), $specific_product_ids ) ) {
        echo '<div class="woocommerce-terms-checkbox">';
        echo '<label>';
        echo '<input type="checkbox" name="accept_terms" id="accept_terms" />';
        echo ' I have read and accept the <a href="/terms-conditions" target="_blank">terms and conditions</a>';
        echo '</label>';
        echo '</div>';
    }
}

// Validate Terms Checkbox before adding to cart
add_filter( 'woocommerce_add_to_cart_validation', 'validate_accept_terms_checkbox', 10, 2 );
function validate_accept_terms_checkbox( $passed, $product_id ) {
    $specific_product_ids = array( 123, 456 ); // Replace with your product IDs

    if ( in_array( $product_id, $specific_product_ids ) && ! isset( $_POST['accept_terms'] ) ) {
        wc_add_notice( 'You must accept the terms and conditions to proceed.', 'error' );
        return false;
    }

    return $passed;
}

SEO Titles

  1. How to Add Terms Checkbox for WooCommerce Products
  2. Require Terms Checkbox Before WooCommerce Add to Cart
  3. Add Custom Checkbox to WooCommerce Product Pages
  4. Require Accept Terms for WooCommerce Product Checkout
  5. Adding Terms Checkbox for Specific WooCommerce Products

Just add the snippet and specify product ID’s in the two spots below

// Add Terms Checkbox on specific product pages
add_action( 'woocommerce_before_add_to_cart_button', 'add_accept_terms_checkbox' );
function add_accept_terms_checkbox() {
    // Define the product IDs that need the checkbox
    $specific_product_ids = array( 123, 456 ); // Replace with your product IDs

    if ( is_product() && in_array( get_the_ID(), $specific_product_ids ) ) {
        echo '<div class="woocommerce-terms-checkbox">';
        echo '<label>';
        echo '<input type="checkbox" name="accept_terms" id="accept_terms" />';
        echo ' I have read and accept the <a href="/terms-conditions" target="_blank">terms and conditions</a>';
        echo '</label>';
        echo '</div>';
    }
}

// Validate Terms Checkbox before adding to cart
add_filter( 'woocommerce_add_to_cart_validation', 'validate_accept_terms_checkbox', 10, 2 );
function validate_accept_terms_checkbox( $passed, $product_id ) {
    $specific_product_ids = array( 123, 456 ); // Replace with your product IDs

    if ( in_array( $product_id, $specific_product_ids ) && ! isset( $_POST['accept_terms'] ) ) {
        wc_add_notice( 'You must accept the terms and conditions to proceed.', 'error' );
        return false;
    }

    return $passed;
}

SEO Titles

  1. How to Add Terms Checkbox for WooCommerce Products
  2. Require Terms Checkbox Before WooCommerce Add to Cart
  3. Add Custom Checkbox to WooCommerce Product Pages
  4. Require Accept Terms for WooCommerce Product Checkout
  5. Adding Terms Checkbox for Specific WooCommerce Products

Leave a Comment

Item added to cart.
0 items - $0