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

标签 php woocommerce field admin orders

我一直在此处进行谷歌搜索,寻找一种将空字段框添加到“编辑订单”页面的简单方法。我们将用它来输入我们 express 公司发货的引用信息。

我们会将其添加到订单注释中,但我们希望它可以搜索,并且还希望将其添加为订单管理页面中的一列(我有管理列插件,我认为可以做到这一点,我只是需要添加此字段才能启动)。

希望有人帮忙,谢谢!

编辑:

我发现这个问题似乎与我正在寻找的问题相似,但更复杂,我不知道如何简化它。 Add a custom meta box on order edit pages and display it on the customer order pages

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

最佳答案

设法得到答案,效果很好!

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

关于php - 如何将自定义字段添加到编辑订单页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61429282/

相关文章:

elasticsearch - elasticsearch 7 中的同义词聚合 - 基于术语

input - 营销人员的 Sitecore Web 表单 : Change Name/ID of fields that is generated

php - 扫描多卷存档中的 .rar 文件内容条目

php - 如何获取 Woocommerce 产品库图片 URL?

php - Woocommerce 中特定产品的购物车批量折扣

php - woocommerce 自定义订单状态和 REST API

java - 按字段对对象数组进行排序

php - 如何使用 php 或 bash 读取文本文件并对其进行排序?

javascript - 如果用户直接在 URL 中输入页面名称,如何重定向页面

php - Paypal 表格与注册表集成