我有一个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 );
发布于 2017-03-28 21:20:43
错误在这里。
<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>
解释
我更改了<input>
标记的value
。
你说逻辑是错的,这是对的。您正在检查$_REQUEST['cheatsheetconfirm']
的值,该值被设置为发布"isconfirmed“。这意味着您的if语句返回false,因为您希望$_REQUEST['cheatsheetconfirm']
等于"isconfirmed“而不是"on”。
如果更改通过表单提交的值,则If语句将返回true。
https://stackoverflow.com/questions/42786203
复制相似问题