在Woocommerce的“管理订单详细信息”部分上显示自定义结帐字段值

时间:2018-08-30 13:53:23

标签: wordpress woocommerce field checkout hook-woocommerce

您好,我正在尝试在管理订单详细信息页面中显示自定义结帐字段。我的自定义字段为 Delivery Option ,它允许用户选择来从复选框中选择一个值。我在下面的类似主题之后使用下面的代码,但是我的代码似乎有问题。

add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_additional_field', 20 );
function checkout_shipping_additional_field()
{
    $domain  = 'wocommerce';
    $default = 'option 1';

    echo '<tr class="additional-shipping-fields"><th>' . __('Delivery Time', $domain) . '</th><td>';

    // Add a custom checkbox field
    woocommerce_form_field( 'custom_radio_field', array(
        'type' => 'select',
        'class' => array( 'form-row-wide' ),
        'options' => array(
            'option 1' => __('10:04 : 13:04 ', $domain),

        ),
        'default' => $default,
    ), $default );

    echo '</td></tr>';
}

//update order meta
add_action('woocommerce_checkout_update_order_meta', 'gon_update_order_meta_business_address');

function gon_update_order_meta_business_address( $order_id ) {
    if ($_POST['custom_radio_field']) update_post_meta( $order_id, 'Business Address?', 
    esc_attr($_POST['custom_radio_field']));
}

// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
    $delivery_time = get_post_meta( $order->get_id(), 'Delivery Time', true );
    if( ! empty( $delivery_time ) )
        echo '<p><strong>'.__('Delivery Time', 'woocommerce').': </strong> ' . $delivery_time . '</p>';
}

1 个答案:

答案 0 :(得分:1)

有一些错误,因此我重新审视了您的代码。我还更换了一些挂钩。请尝试以下操作:

// HERE set your the options array for your select field.
function delivery_time_options(){
    $domain = 'woocommerce';
    return array(
        '1' => __('10:04 : 13:04 ', $domain),
        '2' => __('14:04 : 16:04 ', $domain), // <== Added for testing
    );
}

// Display a custom select field after shipping total line
add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_additional_field', 20 );
function checkout_shipping_additional_field(){
    $domain = 'woocommerce';

    echo '<tr class="additional-shipping-fields"><th>' . __('Delivery Time', $domain) . '</th><td>';

    // Add a custom select field
    woocommerce_form_field( 'delivery_time', array(
        'type' => 'select',
        'class' => array( 'form-row-wide' ),
        'options' => delivery_time_options(),
    ), '' );

    echo '</td></tr>';
}

// Save custom field as order meta data
add_action('woocommerce_checkout_create_order', 'save_custom_field_order_meta', 22, 2 );
function save_custom_field_order_meta( $order, $data ) {
    if ( isset($_POST['delivery_time']) ) {
        $options    = delivery_time_options(); // Get select options array
        $option_key = esc_attr($_POST['delivery_time']); // The selected key

        $order->update_meta_data( '_delivery_time', $options[$option_key] ); // Save
    }
}

// Display a custom field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_custom_meta_data_in_backend_orders', 10, 1 );
function display_custom_meta_data_in_backend_orders( $order ){
    $domain = 'woocommerce';

    $delivery_time = $order->get_meta('_delivery_time');
    if( ! empty( $delivery_time ) )
        echo '<p><strong>'.__('Delivery Time', $domain).': </strong> ' . $delivery_time . '</p>';
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。