Force client to check. Acknowledgment that they have read the document

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 Confirmation Checkbox on Product Single  
add_action( 'woocommerce_single_product_summary', 'woocommerce_cheat_sheet_confirm', 29 );  
    function woocommerce_cheat_sheet_confirm() {
        global $post;
        $cheat_sheet = get_field('cheat_sheet'); // Get Advanced Custom Field
        if ( ! empty( $cheat_sheet ) ) { // If it exists, add the confirmation box ?>                   
            <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 );
+4
1

.

<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>

<input id="cheatsheetconfirm" name="cheatsheetconfirm" type="checkbox" value="on"> I confirm that I have read the <a href="<?php echo $cheat_sheet['url']; ?>">cheat sheet</a>

value <input>.

, . $_REQUEST['cheatsheetconfirm'], "isconfirmed". , if false, , $_REQUEST['cheatsheetconfirm'] "isconfirmed", "on".

, , if true.

+5

Source: https://habr.com/ru/post/1672246/


All Articles