php - Filter order list in woocommerce by custom postmeta field
Get the solution ↓↓↓I need to filter my orders on the WooCommerce Order List by a custom meta field I have set up named custom_company.
I use the following code in my functions.php file to display an additional field for each order. This works fine.
/**
* Adds 'custom_company' column header to 'Orders' page immediately after 'Order Status' and before 'Total' column.
*
* @param array $columns
* @return array
*/
function wc_orders_add_custom_company_column($columns)
{
$new_columns = [];
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_status' === $column_name ) { // Change order_status to manage column orders
$new_columns['custom_company'] = 'Company';
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wc_orders_add_custom_company_column', 20 );
/**
* Adds 'custom_company' column content to 'Orders' page
*
* @param string $column name of column being displayed
*/
function wc_orders_add_custom_company_column_content($column)
{
global $post;
$order = wc_get_order( $post->ID );
$user_id = $order->get_customer_id(); // or $order->get_customer_id();
$meta = get_post_meta($post->ID, 'custom_company', true);
if ( 'custom_company' === $column ) {
echo $meta ? $meta : 'Not Valid!';
}
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_orders_add_custom_company_column_content' );
Now I would like to be able to filter all orders on the order list page based on the values of that postmeta field. This field can contain three different values 'Company A', 'Company B' or 'Company C'.
My goal is to be able to display only orders from a certain company on the orders page in WooCommerce.
How can I go about that?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.