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

时间:2020-04-14 13:08:27

标签: php wordpress woocommerce hook-woocommerce

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

    // 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

非常感谢

1 个答案:

答案 0 :(得分:1)

您将delivery_datecity_customCity混合使用,这给您带来了不同的价值,所以这就是您的问题

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