向WooCommerce结帐中添加新的自定义字段 [英] Add a new custom field to WooCommerce checkout

查看:50
本文介绍了向WooCommerce结帐中添加新的自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应该工作的代码,但是由于某种原因,它没有工作.我想使用以下代码在Woocommerce结帐下添加一个名为城市"的新自定义字段:

I have the code that should work, but for some reason it doesnt. I want to add a new custom field called "City" under Woocommerce checkout, using this code:

    // Display a custom checkout select field after Billing form
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field">
    ' . __('City') . '';

    woocommerce_form_field( 'delivery_date', array(
        'type'          => 'select',
        'options'     => array(
            'Colombo 01' => __('Colombo 01', 'woocommerce' ),
            'Colombo 02' => __('Colombo 02', 'woocommerce' ),
            'Colombo 03' => __('Colombo 03', 'woocommerce' ),
            'Colombo 04' => __('Colombo 04', 'woocommerce' ),
            'Colombo 06' => __('Colombo 06', 'woocommerce' ),
            'Colombo 07' => __('Colombo 07', 'woocommerce' ),
            'Colombo 08' => __('Colombo 08', 'woocommerce' ),
            'Colombo 09' => __('Colombo 09', 'woocommerce' ),
            'Colombo 10' => __('Colombo 10', 'woocommerce' ),
            'Colombo 11' => __('Colombo 11', 'woocommerce' ),
            'Colombo 01' => __('Colombo 01', 'woocommerce' ),
            'Dehiwela' => __('Dehiwela', 'woocommerce' ),
            'Attidiya' => __('Attidiya', 'woocommerce' ),
            'Mount Lavinia' => __('Mount Lavinia', 'woocommerce' ),
            'Ratmalana' => __('Ratmalana', 'woocommerce' ),
            'Maligawatte' => __('Maligawatte', 'woocommerce' ),
            'Kotikawatta' => __('Kotikawatta', 'woocommerce' ),
            'Rajagiriya' => __('Rajagiriya', 'woocommerce' ),
            'Peliyagoda' => __('Peliyagoda', 'woocommerce' ),
            'Kelaniya' => __('Kelaniya', 'woocommerce' ),
            'Kiribathgoda' => __('Kiribathgoda', 'woocommerce' ),
            'Wattala' => __('Wattala', 'woocommerce' )),
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'   => __('Select City'),
        ), $checkout->get_value( 'city_custom' ));

    echo '</div>';
}

// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
    if ( isset($_POST['city_custom']) && ! empty($_POST['city_custom']) ) {
        $order->update_meta_data( 'City', sanitize_text_field( $_POST['city_custom'] ) );
    } 
}

// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
    echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city_custom') . '</p>'; 
}

// Display the custom field value on email notifications:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
    echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city_custom') . '</p>';
} 

但是由于某种原因,我无法查看到后端的订单详细信息(只能看到City作为标签,但没有从客户那里选择数据),也无法在邮件中看到.也可以在其他结算信息中显示此值吗?这里有帮助吗?我使用最新的Woocommerce 4.0.1

but for some reason, i cant see into order details into backend (cust see City as label, but without selected data from customer) , and in mail too.. Also is possible to show this value into other billing inforamtions too? Some help here? i use latest Woocommerce 4.0.1

非常感谢

推荐答案

您使用的是交货日期 city_custom &城市混杂在一起,这为您提供了不同的价值,所以这就是您的问题

You're using delivery_date, city_custom & Citymixed up, this gives you different values, so that is your problem

// Display a custom checkout select field after Billing form
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field">
    ' . __('City') . '';

    woocommerce_form_field( 'city_custom', array(
        'type'          => 'select',
        'options'     => array(
            'Colombo 01' => __('Colombo 01', 'woocommerce' ),
            'Colombo 02' => __('Colombo 02', 'woocommerce' ),
            'Colombo 03' => __('Colombo 03', 'woocommerce' ),
            'Colombo 04' => __('Colombo 04', 'woocommerce' ),
            'Colombo 06' => __('Colombo 06', 'woocommerce' ),
            'Colombo 07' => __('Colombo 07', 'woocommerce' ),
            'Colombo 08' => __('Colombo 08', 'woocommerce' ),
            'Colombo 09' => __('Colombo 09', 'woocommerce' ),
            'Colombo 10' => __('Colombo 10', 'woocommerce' ),
            'Colombo 11' => __('Colombo 11', 'woocommerce' ),
            'Colombo 01' => __('Colombo 01', 'woocommerce' ),
            'Dehiwela' => __('Dehiwela', 'woocommerce' ),
            'Attidiya' => __('Attidiya', 'woocommerce' ),
            'Mount Lavinia' => __('Mount Lavinia', 'woocommerce' ),
            'Ratmalana' => __('Ratmalana', 'woocommerce' ),
            'Maligawatte' => __('Maligawatte', 'woocommerce' ),
            'Kotikawatta' => __('Kotikawatta', 'woocommerce' ),
            'Rajagiriya' => __('Rajagiriya', 'woocommerce' ),
            'Peliyagoda' => __('Peliyagoda', 'woocommerce' ),
            'Kelaniya' => __('Kelaniya', 'woocommerce' ),
            'Kiribathgoda' => __('Kiribathgoda', 'woocommerce' ),
            'Wattala' => __('Wattala', 'woocommerce' )),
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'   => __('Select City'),
        ), $checkout->get_value( 'city_custom' ));

    echo '</div>';
}

// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
    if ( isset($_POST['city_custom']) && ! empty($_POST['city_custom']) ) {
        $order->update_meta_data( 'city', sanitize_text_field( $_POST['city_custom'] ) );
    } 
}

// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
    echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city') . '</p>'; 
}

// Display the custom field value on email notifications:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
    echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city') . '</p>';
} 

这篇关于向WooCommerce结帐中添加新的自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆