繁体   English   中英

如何将自定义字段添加到编辑订单页面

[英]How to add a custom field to the Edit Order page

我一直在谷歌搜索并在这里搜索一种将空字段框添加到“编辑订单”页面的简单方法。 我们将使用它来为我们的快递员提供运输参考。

我们会将其添加到订单备注中,但我们希望它是可搜索的,并且还希望将其添加为 Orders Admin 页面中的一列(我有 Admin Columns 插件,我认为可以做到这一点,我只需要添加此字段开始)。

希望有人能帮忙,谢谢!

编辑:

我发现这个问题看起来很相似,但比我要找的更复杂,我不知道如何简化它。 在订单编辑页面上添加自定义元框并将其显示在客户订单页面上

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

设法得到答案,这很好用!

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

在此处输入图像描述

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

启用自定义字段以获取此选项

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM