Trying to add a custom column to the WooCommerce list of orders in the backend order admin. page; column is inserted but can't seem to get the value from the database. I'm trying to retrieve a value in the wp_woocommerce_order_itemmeta table. I've verified the correct meta key but can't seem to grab the value correctly.
This value is a "shipping speed" choice made by the customer when they place their order (not the standard Woo shipping methods, this is a place where the customer says either "ship my order when everything's complete" or "ship piecemeal as things are ready").
Don't ask how many hours I've burned on this--you don't want to know. Maybe a fresh set of eyes would help.
//add a custom column to the Woo Commerce orders list for "shipping speed"
//create a new column labelled "Shipping Speed
add_filter( 'manage_edit-shop_order_columns', 'add_new_order_admin_list_column' );
function add_new_order_admin_list_column( $columns ) {
$columns['shipping_speed'] = 'Shipping Speed';
return $columns;
}
//populate the column with the correct values for each order
add_action( 'manage_shop_order_posts_custom_column', 'add_new_order_admin_list_column_content' );
function add_new_order_admin_list_column_content( $column ) {
global $post;
if ( 'shipping_speed' === $column ) {
$order = wc_get_order( $post->ID );
$item = $order->get_items();
//get the custom field value from the wp_woocommerce_order_itemmeta table
foreach ( $order->get_items() as $item_id => $item ) {
$custom_field = wc_get_order_item_meta( $item_id, '_wc_checkout_add_on_value', true );
}
//Does the custom field value contain the word 'grinding'?
if ( str_contains( $custom_field, 'grinding' ) ) {
echo "Get Me Grinding";
}
else echo 'no rush';
}
}