我有一个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 );
错误在这里。
<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']
的值,该值设置为" ISConcunded"。这意味着您的if语句返回false,因为您希望 $_REQUEST['cheatsheetconfirm']
等于"确认"而不是"在"。
如果您更改通过表格提交的值,则您的if语句将返回true。