【问题标题】:Add a new custom field to WooCommerce checkout and display on admin order pages and email notifications将新的自定义字段添加到 WooCommerce 结帐并显示在管理订单页面和电子邮件通知中
【发布时间】:2020-04-14 13:08:27
【问题描述】:

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

    // 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>';
} 

但由于某种原因,我无法在后端查看订单详细信息(客户将城市视为标签,但没有来自客户的选定数据),也无法在邮件中查看。也可以将此值显示到其他计费信息中吗?这里有帮助吗?我使用最新的 Woocommerce 4.0.1

非常感谢

【问题讨论】:

标签: php wordpress woocommerce hook-woocommerce checkout


【解决方案1】:

您正在使用 delivery_date、city_custom 和 City混合使用,这给了您不同的值,所以这就是您的问题

// 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>';
} 

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 2019-04-14
    • 1970-01-01
    • 2015-10-08
    • 2020-09-09
    • 2023-03-18
    • 2018-10-01
    • 2019-09-22
    • 2020-06-25
    相关资源
    最近更新 更多