首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >强制客户选中确认他们已阅读文档的复选框

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

Stack Overflow用户
提问于 2017-03-14 20:36:11
回答 1查看 276关注 0票数 4

我有一个WooCommerce网站,它有几个产品,由于他们订购了错误的东西而获得了很多退货。我添加了一个“小抄”,客户可以通过高级自定义字段参考。我已经编写了以下代码,以强制他们在将产品添加到购物车之前选中该框以确认他们已经阅读了该框。

问题是,无论他们是否选中该框,我都会得到显示的自定义错误消息。很明显,我的逻辑有一个缺陷,但我不能准确地指出它。

任何帮助都将不胜感激。

代码语言:javascript
运行
复制
<?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 );
EN

回答 1

Stack Overflow用户

发布于 2017-03-28 21:20:43

错误在这里。

代码语言:javascript
运行
复制
<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>

把它改成这样

代码语言:javascript
运行
复制
<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>

解释

我更改了<input>标记的value

你说逻辑是错的,这是对的。您正在检查$_REQUEST['cheatsheetconfirm']的值,该值被设置为发布"isconfirmed“。这意味着您的if语句返回false,因为您希望$_REQUEST['cheatsheetconfirm']等于"isconfirmed“而不是"on”。

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42786203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档