I have a WooCommerce site that has several products that receive a lot of returns because they order the wrong thing. I added a "Cheat Sheet" which the client can reference through additional custom fields. I wrote the code below to make them check the box to confirm that they had read it before they could add the product to the cart.
The problem is that regardless of whether they check the box or not, I get an error. So, obviously, I have a flaw in my logic, but I can’t pinpoint it.
Any help would be greatly appreciated.
<?php
add_action( 'woocommerce_single_product_summary', 'woocommerce_cheat_sheet_confirm', 29 );
function woocommerce_cheat_sheet_confirm() {
global $post;
$cheat_sheet = get_field('cheat_sheet');
if ( ! empty( $cheat_sheet ) ) {
<div id="cheat-sheet-confirmation" class="checkbox">
<form>
<label>
<input id="cheatsheetconfirm" name="cheatsheetconfirm" type="checkbox" value="isconfirmed"> I confirm that I have read the <a href="<?php echo $cheat_sheet['url']; ?>" />cheat sheet</a>.
</label>
</form>
</div>
<?php }
}
function cheatsheetConfirmation() {
if(isset($_REQUEST['cheatsheetconfirm']) && $_REQUEST['cheatsheetconfirm'] == 'on'){
$is_checked = true;
}
else {
$is_checked = false;
}
if ($is_checked == false) {
wc_add_notice( __( 'Please acknowledge that you have read the cheat sheet.', 'woocommerce' ), 'error' );
return false;
}
return true;
}
add_action( 'woocommerce_add_to_cart_validation', 'cheatsheetConfirmation', 10, 3 );