Tất cả các đoạn code sau đều cho vào file functions.php
Đoạn 1, thêm cột vào trong admin
// Add a new custom column to admin order list
add_filter( 'manage_edit-shop_order_columns', 'admin_orders_list_add_column', 10, 1 );
function admin_orders_list_add_column( $columns ){
$columns['custom_column3'] = __( 'Product ID', 'woocommerce' );
$columns['custom_column4'] = __( 'Design Front', 'woocommerce' );
$columns['custom_column5'] = __( 'Design Back', 'woocommerce' );
return $columns;
}
Đoạn 2, thêm dữ liệu cho cột
// The data of the new custom column in admin order list
add_action( 'manage_shop_order_posts_custom_column' , 'admin_orders_list_column_content', 10, 2 );
function admin_orders_list_column_content( $column, $post_id ){
global $the_order;
if( 'custom_column3' === $column ){
$count = 0;
// Loop through order items
foreach( $the_order->get_items() as $item ) {
$product = $item->get_product(); // The WC_Product Object
$id = $product->get_id();
$producteditURL = get_edit_post_link($id);
printf('<a href="%s">%s</a>', $producteditURL, $id);
printf("\n");
$count++;
}
}
if( 'custom_column4' === $column ){
$count = 0;
// Loop through order items
foreach( $the_order->get_items() as $item ) {
$product = $item->get_product(); // The WC_Product Object
//$style = $count > 0 ? ' style="padding-left:6px;"' : '';
// Display product thumbnail
$designFront = get_post_meta($product->id, 'design_front', true);
printf('<a href="%s">%s</a>', $designFront, $designFront);
printf("\n");
$count++;
}
}
if( 'custom_column5' === $column ){
$count = 0;
// Loop through order items
foreach( $the_order->get_items() as $item ) {
$product = $item->get_product(); // The WC_Product Object
//$style = $count > 0 ? ' style="padding-left:6px;"' : '';
// Display product thumbnail
$designBack = get_post_meta($product->id, 'design_back', true);
printf('<a href="%s">%s</a>', $designBack, $designBack);
printf("\n");
$count++;
}
}
}
Để hoạt động được thì trong product phải thêm custom fields
với tên là : design_front
và design_black
. Ko có thì ko hiện chứ ko có lỗi