[英]Add custom checkout fields below the terms and conditions in Woocommerce
I have built an e-commerce site using Woocommerce.我已经使用 Woocommerce 建立了一个电子商务网站。 I would like to add two more check boxes below the terms and conditions.我想在条款和条件下方再添加两个复选框。 I have searched everywhere for a working solution and the only thing that I found is a commercial plugin.我到处寻找可行的解决方案,唯一找到的是商业插件。
How to add custom checkout fields (2 checkboxes) below the terms and conditions programmatically?如何以编程方式在条款和条件下方添加自定义结帐字段(2 个复选框)?
Location of the terms and condition screenshot:条款和条件截图的位置:
The code:代码:
add_action('woocommerce_checkout_before_terms_and_conditions', 'checkout_additional_checkboxes');
function checkout_additional_checkboxes( ){
$checkbox1_text = __( "My first checkbox text", "woocommerce" );
$checkbox2_text = __( "My Second checkbox text", "woocommerce" );
?>
<p class="form-row custom-checkboxes">
<label class="woocommerce-form__label checkbox custom-one">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_one" > <span><?php echo $checkbox1_text; ?></span> <span class="required">*</span>
</label>
<label class="woocommerce-form__label checkbox custom-two">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_two" > <span><?php echo $checkbox2_text; ?></span> <span class="required">*</span>
</label>
</p>
<?php
}
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['custom_one'] )
wc_add_notice( __( 'You must accept "My first checkbox".' ), 'error' );
if ( ! $_POST['custom_two'] )
wc_add_notice( __( 'You must accept "My second checkbox".' ), 'error' );
}
Code goes in function.php file of your active child theme (active theme).代码位于活动子主题(活动主题)的 function.php 文件中。
Tested and works.测试和工作。
And what would the code that displays an additional checkbox for the selected product category look like?显示所选产品类别的附加复选框的代码是什么样的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.