Woocommerce中的条件自定义结帐字段验证问题

伊娃科斯

我有一个复选框,如果选中该复选框,则会打开一个下拉字段(要求)。如果您尝试发送Woocommerce订单,它将有条件地检查该字段是否包含内容,如果没有,则返回错误。

当需求字段确实有信息输入时,所有这些工作都可以接受,但仍将其视为没有内容并返回错误。

因为这是表单应该工作的基本方式,所以我不明白为什么会得到这样的结果。这是代码:

/**
 * Add a message field to the WC checkout
 */
add_action( 'woocommerce_before_checkout_form', 'custom_checkout_field' );

function custom_checkout_field( $checkout ) {

    echo '<div id="message"><h3>' . __( '<i class="fas fa-envelope"></i> Message' ) . '</h3><p style="margin: 0 0 8px;">Would you like to leave a message?</p>';

        woocommerce_form_field( 'checkbox', array(
            'type'  => 'checkbox',
            'class' => array( 'msg-checkbox' ),
            'label' => __( 'Yes' ),
        ), $checkout->get_value( 'checkbox' ) );

    woocommerce_form_field( 'requirements', array(
        'type'          => 'text',
        'class'         => array('msg'),
        'label'         => __('Please input your message.'),
        'placeholder'   => __(''),
        ), $checkout->get_value( 'requirements' ));

    echo '</div>';

}

/**
 * Process checkout with additional custom field
 */
add_action('woocommerce_checkout_process', 'custom_checkout_field_process');

function custom_checkout_field_process() {

    // Check if set, if not add an error.

    if(isset($_POST['checkbox']) && ( empty($_POST['requirements'])));

        wc_add_notice( __( 'Please let us know what you would like to do' ), 'error' );
}

我希望这是有道理的。基本上它可以工作到一定程度,但是当输入字段包含内容时,对输入字段的验证不起作用。

谢谢。

LoicTheAztec

更新:代码改进,并使用jQuery添加了显示/隐藏功能

唯一的问题来自用于自定义结帐字段的挂钩。它在结帐表单之外输出这些字段,因此从不提交值,然后始终为空。

取而代之的是,您将对woocommerce_checkout_before_customer_details第一个挂钩函数使用动作挂钩,这一次将在结帐表单中输出这些字段

// Output custom checkout fields
add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_field_before_billing' );
function custom_checkout_field_before_billing() {
    $domain = 'woocommerce';

    // Hide the message text field on load
    ?>
    <style>p#requirements_field{display:none;}</style>

    <div id="message">
    <h3><i class="fa fa-envelope"></i><?php _e( 'Message', 'woocommerce' ); ?></h3>
    <?php

    woocommerce_form_field( 'checkbox_msg', array(
        'type'  => 'checkbox',
        'class' => array( 'msg-checkbox' ),
        'label' => __( 'Would you like to leave a message?', 'woocommerce' ),
    ), WC()->checkout->get_value( 'cb_msg' ));

    woocommerce_form_field( 'requirements', array(
        'type'              => 'text',
        'class'             => array('msg t_msg'),
        'label'             => __('Please input your message.'),
        'placeholder'       => __(''),
    ), WC()->checkout->get_value( 'requirements' ));

    echo '</div>';

    // jQuery: show hide the text requirement field
    ?><script>
    jQuery(document).ready(function($) {
        var a = '#requirements_field';
        $('input#checkbox_msg').change( function(){
            if( $(this).is(':checked') )
                $(a).show();
            else
                $(a).hide();
        });
    });
    </script><?php
}

// Process checkout with additional custom field
add_action('woocommerce_after_checkout_validation', 'custom_checkout_field_validation_process', 20, 2 );
function custom_checkout_field_validation_process( $data, $errors ) {

    // Check if set, if not add an error.
    if( isset($_POST['checkbox_msg']) && empty($_POST['requirements']) )
        $errors->add( 'requirements', __( "Please let us know what you would like to do filling up the message field.", "woocommerce" ) );
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Woocommerce 3中的自定义结帐字段验证

WooCommerce:结帐验证失败后更新自定义字段

Woocommerce自定义结帐必填字段行为问题

在Woocommerce中启用自定义结帐电子邮件字段验证

Woocommerce结帐页面中的货运公司自定义字段验证

在WooCommerce中添加和处理自定义结帐选择字段时出现问题

在WooCommerce中取消有条件的自定义结帐字段

结帐页面WooCommerce中的自定义交付字段

在woocommerce结帐页面自定义字段中添加日期

从WooCommerce的自定义结帐计费字段中获取价值

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

Woocommerce中的自定义结帐随机选择字段

在WooCommerce中清理自定义结帐字段数据

结帐中的Woocommerce自定义产品字段

WooCommerce中的动态同步自定义结帐选择字段

在Woocommerce中捕获自定义结帐字段值

在WooCommerce中添加自定义注册字段和电话字段验证问题

在Woocommerce中添加自定义结帐字段时,结帐页为空白

从 Woocommerce 结帐中的自定义产品字段值中填写选择字段选项

基于Woocommerce中产品类别的条件自定义结帐字段

WooCommerce-不同人自定义状态的结帐条件字段

根据免费送货条件隐藏Woocommerce结帐自定义字段

有条件地自定义WooCommerce结帐字段

保存将值合并到WooCommerce中现有字段的结帐自定义字段

在结帐页面和WooCommerce数据字段中添加取件地点自定义字段

调用并显示WooCommerce自定义结帐字段值

向Woocommerce结帐添加新的自定义字段

重新排列和自定义woocommerce结帐字段

使Woocommerce自定义结帐字段不可编辑