如何在 "编辑订单 "页面添加自定义字段?

问题描述 投票:0回答:1

我一直在谷歌和这里搜索一个简单的方法来添加一个空字段框到编辑订单页面。 我们将用它来输入我们的快递公司提供的货物参考。

我们会把它添加到订单备注中,但我们希望它可以被搜索到,同时也想把它添加到订单管理页面中作为一栏(我有Admin Columns插件,我认为可以做到这一点,我只需要添加这个字段开始)。

希望有人能帮忙,谢谢!

EDIT

我发现这个问题似乎是类似的,但更复杂,比我正在寻找什么,我不知道如何简化它。在订单编辑页上添加自定义元宝箱,并在客户订单页上显示出来

我不需要任何像这样在前端显示给客户的东西。只需要一个简单的空框,显示在每个订单编辑页面上(也许在订单备注下面),可以搜索到。然后我也会在订单管理页面的一栏中显示出来。

php woocommerce field admin orders
1个回答
2
投票

管理得到一个答案,这是伟大的工作!

//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

// For displaying in columns.

add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
    $columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
    return $columns;
}

// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
    switch ( $column ) {

        case 'custom_column' :
            echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
            break;

    }
}

// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {

    add_meta_box(
        'custom_column',
        __( 'Custom Column', 'your_text_domain' ),
        'shop_order_display_callback',
        'shop_order'
    );

}

// For displaying.
function shop_order_display_callback( $post ) {

    $value = get_post_meta( $post->ID, 'custom_column', true );

    echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}

// For saving.
function save_shop_order_meta_box_data( $post_id ) {

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
            return;
        }
    }

    // Make sure that it is set.
    if ( ! isset( $_POST['custom_column'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['custom_column'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, 'custom_column', $my_data );
}

add_action( 'save_post', 'save_shop_order_meta_box_data' );


1
投票

enter image description here

使用默认的WooCommerce自定义字段在订单编辑页面底部部分。

enable custom field to get this option

© www.soinside.com 2019 - 2024. All rights reserved.