I'm trying to show a custom field added to all orders made from the website's front-end called "Handleby" with the value "frontend" via a custom column on the admin's orders page. I was able to create a custom column, but couldn't populate it with my field's value.
I'm following this guide from SkyVerge, for WooCommerce 3.0+
This creates the custom field and automatically assign it to orders made from front-end checkout:
/* Add custom field for orders created from the front-end */
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
$order->update_meta_data( 'Handleby', 'frontend' );
}
This creates the custom column:
/**
* Add column "Handled By" on orders page to filter for front-end orders
*
* @param string[] $columns
* @return string[] $new_columns
*/
function add_order_handleby_column_header($columns) {
$new_columns = array();
foreach($columns as $column_name => $column_info) {
$new_columns[ $column_name ] = $column_info;
// Create a new column named "Handled By" after the Status column
if('order_status' === $column_name) {
$new_columns['order_handleby'] = __('Handled By', 'my-textdomain');
}
}
return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'add_order_handleby_column_header', 20);
/* End add "Handled By" column */
This is the helper function to get the meta from "Handleby":
/* Helper function used to get custom meta "Handleby" */
if(!function_exists('get_order_handleby_meta')) :
/**
* Function to get meta from an order
*
* @param \WC_Order $order the order
* @param string $key the meta key
* @param bool $single whether to get the meta as a single item. Defaults to 'true'
* @param string $context if 'view' then the value will be filtered
* @return mixed the order property
*/
function get_order_handleby_meta($order, $key = '', $single = true, $context = 'edit') {
// For WooCommerce 3.0 or later
if(defined('WC_VERSION') && WC_VERSION && version_compare(WC_VERSION, '3.0', '>=')) {
$value = $order->get_meta($key, $single, $context);
} else {
// Have $order->get_id() check here in case WC_VERSION isn't defined correctly
$order_id = is_callable(array($order, 'get_id')) ? $order->get_id() : $order->id;
$value = get_post_meta($order_id, $key, $single);
}
return $value;
}
endif;
/* End of helper function to get meta */
This tries to populate the column with the meta values. I believe this is where I went wrong:
/**
* Adds "Handled By" column content to the orders page after the Status column
*
* @param string[] $column name of column being displayed
*/
function add_order_handleby_column_content($column) {
global $post;
if('order_handleby' === $column) {
$order = wc_get_order($post->ID);
$handleby = get_order_handleby_meta($order, 'Handleby');
echo $handleby;
}
}
add_action('manage_shop_order_posts_custom_column', 'add_order_handleby_column_content');
/* End of adding column content */
And lastly, this styles the column:
/**
* Adjust the appearance for the new "Handle By" column.
*/
function add_order_handleby_column_style() {
$css = '.widefat .column-order_status, .widefat .column-order_handleby { width: 9%; }';
wp_add_inline_style('woocommerce_admin_styles', $css);
}
add_action('admin_print_styles', 'add_order_handleby_column_style');
echo $handleby
is probably where I went wrong.
If anyone can point me to the right direction, that'd be most helpful. Thank you!