首页 文章

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

提问于
浏览
1

我在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. changing:

if ( ! $_POST['developer_name'] )

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

2. changing the trigger from:

add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );

add_action( 'woocommerce_after_checkout_form', 'my_custom_checkout_fields' );

3. updating to latest Woocomerce 3.0.5 version

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

2 回答

  • 1

    正如你可以在 woocommerce_before_checkout_form 钩子中读到的那样,它是在结账表格之前(所以在结账表格之外) . For this reason this custom field can't work in this hook .

    您可以使用 woocommerce_checkout_update_order_meta action钩子,在代码中进行一些小的更改,因为它没有可用的$ 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版

  • 3

    你试试这个代码 . 测试好了

    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

相关问题