必需的自定义WooCommerce结帐字段不验证输入的值

时间:2017-05-01 07:44:06

标签: php wordpress woocommerce checkout custom-fields

我在Storefront子主题 functions.php 文件中添加了WooCommerce自定义结帐字段。
他们有一个" required"属性。
目的是让这些字段显示在页面顶部,在结算字段之前。

点击提交按钮继续付款时,我收到了所需的自定义字段验证错误('请填写您的姓名')并且无法继续付款,甚至尽管用有效数据填充该字段。

如何修复此问题或从何处开始调试?

这是functions.php中的代码:

add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );

function my_custom_checkout_fields( $checkout ) {

    echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';

    woocommerce_form_field( 'developer_name', array(
        'type'          => 'text',
        'class'         => array('developer_name-class form-row form-row-first'),
        'label'         => __('name'),
        'placeholder'   => __('fill in your name'), 
        'required'      => true,
        ), $checkout->get_value( 'developer_name' ));                               

    echo '</div>';

}

/**
 * Process the checkout
 */
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['developer_name'] )
        wc_add_notice( __( 'please fill in your name' ), 'error' );  
}

我试过以下但没有一个帮助过:

1。改变:

if ( ! $_POST['developer_name'] ) 

if ( empty( $_POST['developer_name']) )

2。更改触发器:

 add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' ); 

add_action( 'woocommerce_after_checkout_form', 'my_custom_checkout_fields' );

第3。更新到最新的Woocomerce 3.0.5版本

  

我正在运行Wordpress 4.7.4
  其他相关的活动插件:
  Uni CPO - WooCommerce选项和价格计算公式

3 个答案:

答案 0 :(得分:3)

正如您可以在 woocommerce_before_checkout_form 挂钩中看到的那样,它是在结帐表单之前(因此在结帐表单之外)。 因此,此自定义字段无法在此挂钩中使用

您可以使用 woocommerce_checkout_update_order_meta 操作挂钩,在代码中进行一些小更改,因为其中没有可用的$ checkout参数。

  

这会在结算字段之前的页面顶部显示“字段 ...

enter image description here

所以你的完整代码现在应该是:

/**
 * Add the field to the checkout
 */
add_action( 'woocommerce_checkout_before_customer_details', 'my_custom_checkout_fields' );

function my_custom_checkout_fields() {

    echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';

    woocommerce_form_field( 'developer_name', array(
        'type'          => 'text',
        'class'         => array('developer_name-class form-row form-row-first'),
        'label'         => __('name'),
        'placeholder'   => __('fill in your name'),
        'required'      => true,
    ), WC()->checkout->get_value( 'developer_name' ));

    echo '</div>';

}

/**
 * Process the checkout
 */
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['developer_name'] )
        wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}


// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['developer_name'] ) ) {
        update_post_meta( $order_id, 'Developer name', sanitize_text_field( $_POST['developer_name'] ) );
    }
}

// Display the custom-field in orders view
add_action( 'woocommerce_order_details_after_customer_details', 'display_my_custom_field_in_orde_details', 10, 1 );
function display_my_custom_field_in_orde_details( $order ) {

    $developer_name = get_post_meta( $order->get_id(), 'Developer name',  true );

    if ( ! empty( $developer_name ) ):
    ?>
        <table class="woocommerce-table woocommerce-table--customer-details shop_table customer_details">
            <tbody><tr>
                <th>Developer name:</th>
                <td><?php echo $developer_name; ?></td>
            </tr></tbody>
        </table>
    <?php
    endif;
}

此代码包含活动子主题(或主题)的function.php文件或任何插件文件。

此代码经过测试,适用于WooCommerce版本3.0 +

答案 1 :(得分:1)

您尝试使用此代码。测试好了

add_action( 'woocommerce_billing_fields', 'my_custom_checkout_fields' );

function my_custom_checkout_fields( $fields ) {

   $fields['billing_developer_name'] = array(
    'label'       => __('Developer name', 'woocommerce'),            
    'placeholder' => _x('Developer name', 'placeholder', 'woocommerce'),  
    'required'    => TRUE,             
    'clear'       => false,             
    'type'        => 'text',              
    'class'       => array('my-css')    
    );

 return $fields;

}

您可以使用此代码段

进行排列
add_filter("woocommerce_checkout_fields", "order_fields");

function order_fields($fields) {

    $order = array(
        "billing_developer_name",
        "billing_first_name", 
        "billing_last_name", 
        "billing_company", 
        "billing_address_1", 
        "billing_address_2", 
        "billing_postcode", 
        "billing_country", 
        "billing_email", 
        "billing_phone"

    );
    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;
    return $fields;

}

请参阅screenshot

答案 2 :(得分:0)

**/* If You Have Created Your Custom Field at the checkout page */**

add_action( 'woocommerce_after_checkout_validation', 'shipping_time_optionss', 9999, 2);
function shipping_time_optionss( $fields, $errors ){
    // if any validation errors
    if ( empty( $_POST['woo_shipping_time'] ) ) {
        $errors->add( 'woocommerce_password_error', __( 'Please Select Shipping Time Option.' ) );
    } `enter code here`
}