A freelance client sells two distinct products on the same website: a membership and an online course. Two different audiences, different formats and… different Terms & Conditions.
The goal was therefore to display the “Terms & Conditions” checkbox on the Checkout page based on the product in the cart. Once again, we’re going to use Conditional Logic. With that, the snippet is pretty easy to code!

PHP Snippet: Terms & Conditions by Product – WooCommerce Checkout
/** * @snippet Terms & Conditions by Product - WooCommerce Checkout * @how-to Watch tutorial @ https://businessbloomer.com/?p=19055 * @sourcecode https://businessbloomer.com/?p=72828 * @author Rodolfo Melogli * @testedwith WooCommerce 3.3.1 */ add_action( 'woocommerce_review_order_before_submit', 'bbloomer_add_checkout_per_product_terms', 9 ); function bbloomer_add_checkout_per_product_terms() { // Show Terms 1 $product_id_1 = 522; $product_cart_id_1 = WC()->cart->generate_cart_id( $product_id_1 ); $in_cart_1 = WC()->cart->find_product_in_cart( $product_cart_id_1 ); if ( $in_cart_1 ) { ?> <p class="form-row terms wc-terms-and-conditions"> <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox"> <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="terms-1" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms-1'] ) ), true ); ?> id="terms-1"> <span>I agree to <a href="___" target="_blank">terms-1</a></span> <span class="required">*</span> </label> <input type="hidden" name="terms-1-field" value="true"> </p> <?php } // Show Terms 2 $product_id_2 = 2152; $product_cart_id_2 = WC()->cart->generate_cart_id( $product_id_2 ); $in_cart_2 = WC()->cart->find_product_in_cart( $product_cart_id_2 ); if ( $in_cart_2 ) { ?> <p class="form-row terms wc-terms-and-conditions"> <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox"> <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="terms-2" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms-2'] ) ), true ); ?> id="terms-2"> <span>I agree to <a href="____" target="_blank">terms-2</a></span> <span class="required">*</span> </label> <input type="hidden" name="terms-2-field" value="true"> </p> <?php } } // Show notice if customer does not tick either terms add_action( 'woocommerce_checkout_process', 'bbloomer_not_approved_terms_1' ); add_action( 'woocommerce_checkout_process', 'bbloomer_not_approved_terms_2' ); function bbloomer_not_approved_terms_1() { if ( $_POST['terms-1-field'] == true ) { if ( empty( $_POST['terms-1'] ) ) { wc_add_notice( __( 'Please agree to terms-1' ), 'error' ); } } } function bbloomer_not_approved_terms_2() { if ( $_POST['terms-2-field'] == true ) { if ( empty( $_POST['terms-2'] ) ) { wc_add_notice( __( 'Please agree to terms-2' ), 'error' ); } } }
It appears that this only works if both checkboxes are visible and checked. So if a customer is buying only one product, and the checkout page shows only one checkbox, the form won’t submit because it wants the missing 2nd checkbox to be checked. This is the expected behavior?
You’re right Arp ๐ Snippet has now been revised.
Thanks!
In the last part where you show a notice if user doesn’t check the box:
add_action(‘woocommerce_checkout_process’, ‘bbloomer_not_approved_terms_1’);
… it is checking for that value every time a user goes to Checkout.
If user didn’t add these items to their Cart, it shouldn’t tell them they didn’t tick the box.
We need to add the product checking logic into this function as well, or else the Checkout will always be looking for the field values.
Would you recommend writing a reusable function which will check if the product is in the cart? I’m thinking it would return true if product is in there. Then we could write an if() statement call to it from both actions. Is that correct?
Something like this:
???
Alex, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
I’m seeing this too. I set it up two different products, like the demo. If one is in the cart, the function is still checking to see if BOTH checkboxes are checked.
So this solution only works if both products are in the cart.
Hi Rodolfo,
Your articles have been incredibly helpful – thank you! On the above, the second set of terms only worked for me when I removed the “a” from “if ( $in_cart_2a )”
Thanks again!
Thank you Heather! Typo has now been removed, you’re a star ๐
Hi Rodolfo —
Thank you so much for this code! It’s working great with my simple products. I also have a variable product that I’d like to setup. How would I specify that product ID? When I set the $product_id_2 = 6833 (the variation ID), it does not work. Is there additional code to accommodate a variable product?
Thanks,
Kari
Hey Kari, thanks so much for your comment! Each variation has a common parent ID, you should use that ๐
Hi Rodolfo — I should have mentioned that I tried using the parent product and that also did not work. It seems like when you’re checking what’s in the cart, you’d have to specify not only the parent product ID, but also the variation ID?
Thanks,
Kari
Uhm, maybe the find_product_in_cart only works with simple product? You can check if a product is in the cart with a foreach alternatively, like I did here: https://businessbloomer.com/woocommerce-apply-coupon-programmatically-product-cart/
Hi Rodolfo,
Thank you very much.
I love your Visual Guides.
About this snippet, I am a newbie in PHP, and I don’t understand 100% the line:
if ( ! empty( $_POST[‘terms-2’] ) && empty( $_POST[‘terms-2’] ) )
I think is like “If not terms-2 is empty and terms-2 is empty” You can help me with that? How I must interpret it?
Regards,
Urano
Oops, you’re right Urano – snippet updated ๐ Thank you!
Please help i have tried to use it in storefront theme but it isnt working for me
Pankaj, thanks so much for your comment! Unfortunately this is custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Hi, maybe you have problem because there are conditions only for products with id 522 and 2152
$product_id_2 = 2152;
$product_id_1 = 522;
Just delete the whole condition ๐