Here is solution for above questions
// Custom function to get the product names, that appear in the existing orders
function get_product_names() {
global $wpdb;
return $wpdb->get_col("
SELECT DISTINCT order_item_name
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_type = \'line_item\'
");
}
function action_restrict_manage_posts( $post_type, $which ) {
global $pagenow;
// Compare
if ( $post_type === \'shop_order\' && $pagenow === \'edit.php\' ) {
$filter_id = \'filter_product\';
$current = isset( $_GET[$filter_id] ) ? $_GET[$filter_id] : \'\';
// Create a drop-down list
echo \'<select name="\' . $filter_id . \'">
<option value="">\' . __( \'Filter by product\', \'woocommerce\' ) . \'</option>\';
foreach ( get_product_names() as $product_name ) {
printf( \'<option value="%s"%s>%s</option>\', $product_name, $product_name === $current ? \'" selected="selected"\' : \'\', ucfirst( $product_name ) );
}
echo \'</select>\';
}
}
add_action( \'restrict_manage_posts\', \'action_restrict_manage_posts\', 10, 2 );
function action_pre_get_posts( $query ) {
global $pagenow, $post_type, $wpdb;
$filter_id = \'filter_product\';
// Compare
if ( $query->is_admin && $pagenow === \'edit.php\' && $post_type === \'shop_order\' && isset( $_GET[$filter_id] ) && $_GET[$filter_id] != \'\' ) {
// Get all orderIDs in which the product name occurs
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_type = \'line_item\'
AND order_item_name = \'%s\'
", esc_attr( $_GET[$filter_id] )
) );
// Set the new "meta query"
$query->set( \'post__in\', $order_ids );
// Set "posts per page"
$query->set( \'posts_per_page\', 20 );
// Set "paged"
$query->set( \'paged\', ( get_query_var(\'paged\') ? get_query_var(\'paged\') : 1
) );
}
}
add_action( \'pre_get_posts\', \'action_pre_get_posts\', 10, 1 );