简体   繁体   English

在 Woocommerce 中的条款和条件下方添加自定义结帐字段

[英]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 1st hooked function displays the 2 additional checkout fields第一个挂钩函数显示 2 个额外的结帐字段
  • The 2nd hooked function will check that both checkboxes are "selected" to allow checkout, displaying a custom error notice if not…第二个挂钩函数将检查两个复选框是否都被“选中”以允许结帐,如果没有,则显示自定义错误通知......

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.

相关问题 Woocommerce 条款和条件链接 - 结帐 - Woocommerce Terms and conditions link - checkout 在 WooCommerce 中有条件地删除结帐条款和条件 - Remove Checkout Terms & Conditions conditionally in WooCommerce 内联错误内联条款和条件@ WooCommerce 结帐 - Inline Errors Inline with Terms & conditions @ WooCommerce Checkout 在 Woocommerce Checkout 中的帐单国家/地区下方添加自定义字段 - Add a custom field below billing country in Woocommerce Checkout 如何在woocommerce结帐页面上的产品表下方添加自定义短代码 - How to add a custom shortcode below the product table on woocommerce checkout page 在结帐页面和 WooCommerce 数据字段中添加取货地点自定义字段 - Add pickup locations custom field on checkout page and in WooCommerce data fields 如果只有非虚拟物品,则添加 WooCommerce 自定义结帐字段 - Add WooCommerce custom checkout fields if there is only non virtual items 在 WooCommerce 中将结帐自定义字段数据添加到运输包裹中进行计算 - Add checkout custom fields data to shipping packages for calculations in WooCommerce 自定义验证WooCommerce结帐字段 - Custom validation of WooCommerce checkout fields 在结帐中取消设置自定义字段-woocommerce - unsetting custom fields in checkout - woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM