Dưới đây là cách lấy ghi chú mới nhất từ bảng wp_comments và hiển thị trong cột “Ghi chú”:
// Thêm cột Ghi chú vào danh sách đơn hàng
if (class_exists('WooCommerce')) {
// Thêm cột Ghi chú vào danh sách đơn hàng
add_filter('manage_edit-shop_order_columns', 'add_notes_column_to_orders');
function add_notes_column_to_orders($columns) {
$new_columns = [];
foreach ($columns as $key => $column) {
$new_columns[$key] = $column;
if ('order_date' === $key) { // Thêm cột sau cột "Ngày đặt hàng"
$new_columns['order_notes'] = 'Ghi chú';
}
}
return $new_columns;
}
// Hiển thị nội dung cột Ghi chú
add_action('manage_shop_order_posts_custom_column', 'show_latest_notes_column_content', 10, 2);
function show_latest_notes_column_content($column, $post_id) {
if ('order_notes' === $column) {
global $wpdb;
// Lấy ghi chú mới nhất từ bảng wp_comments
$latest_note = $wpdb->get_var(
$wpdb->prepare("
SELECT comment_content
FROM {$wpdb->comments}
WHERE comment_post_ID = %d AND comment_type = 'order_note'
ORDER BY comment_date DESC
LIMIT 1
", $post_id)
);
// Hiển thị ghi chú hoặc thông báo không có ghi chú
echo $latest_note ? esc_html($latest_note) : '<em>Không có ghi chú</em>';
}
}
}
Chào ! Bạn thấy nội dung này thế nào?





