我一直在谷歌上搜索,并在这里搜索一种简单的方法来向“编辑订单”页面添加一个空字段框。我们将以此为参考,以从我们的快递公司寄出货物。
我们会将其添加到订单注释中,但我们希望它可以被搜索,并且还希望将其添加为“订单管理”页面中的一列(我有“管理列”插件,我认为可以做到这一点,我只是需要添加此字段才能开始)。
希望有人可以提供帮助,谢谢!
编辑:
我发现这个问题似乎比我要寻找的问题相似,但更为复杂,我不知道该如何简化。 Add a custom meta box on order edit pages and display it on the customer order pages
我不需要像这样在前端显示任何内容。只是一个简单的空框,它显示在可以搜索的每个订单编辑页面(可能在订单注释下方)上。然后,我也将其显示在订单管理页面上的列中。
答案 0 :(得分: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 :(得分:1)