强制客户选中复选框,确认他们已阅读文档

Tri*_*eft 4 php wordpress product woocommerce advanced-custom-fields

我有一个WooCommerce网站,有几个产品由于他们订购了错误的东西而获得了大量的回报.我添加了一个"备忘单",客户可以通过高级自定义字段引用该备忘单.我已经编写了下面的代码,强制他们选中复选框以确认他们已经阅读了它,然后才能将产品添加到购物车中.

问题是,无论是否勾选方框,我都会显示自定义错误消息.显然我的逻辑有一个缺陷,但我无法确定它.

任何帮助将不胜感激.

<?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 );
Run Code Online (Sandbox Code Playgroud)

Jus*_*rty 5

错误在这里.

<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>
Run Code Online (Sandbox Code Playgroud)

把它改成这个

<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>
Run Code Online (Sandbox Code Playgroud)

说明

我改变value<input>标签.

你说错了逻辑是正确的.您正在检查其值$_REQUEST['cheatsheetconfirm']设置为发布"isconfirmed".这意味着你的if语句返回false,因为你想要$_REQUEST['cheatsheetconfirm']等于"isconfirmed"而不是"on".

如果更改通过表单提交的值,则if语句将返回true.