I am trying to filter the list of WooCommerce Orders by the Pickup Location selected by the customer at checkout. I am using the Local Pickup Plus plugin. A column for the pickup location is available in the admin Orders list but I am having trouble with getting the meta_query to filter the list by Pickup Location.

The Pickup Location dropdown filter appears and is included in the post data but using the filter always gets 0 results. I am thinking I am either not sending the correct post info for the selected Pickup Location, I am not looking at the correct Meta item, or a combo of both.

What should the pickup_location_filter option value be?
I have tried the pickup location ID and the text that is saved to the Pickup Location meta info in the order.

What should the $meta_key_query key be set to?

function orders_pickup_location_filters( $post_type ) {

if( $post_type == 'shop_order' ) {

  echo '<select name="pickup_location_filter">';

    echo '<option value>Select a Pickup Location</option>';
	$pickup_location_list = get_option( 'woocommerce_pickup_locations' );
	foreach($pickup_location_list as $key => $value) {
	
		 $pickup_location_string = $value['company'].', '.$value['address_1'].' '.$value['address_2'].', '.$value['city'].', '.$value['state'].' '.$value['postcode'].', '.$value['phone'];
		
		echo '<option value="'.$pickup_location_string.'">'.$value['company'].'</option>';
	}
  	echo '</select>';

}

}

add_action( 'restrict_manage_posts', 'orders_pickup_location_filters' );

function apply_my_custom_product_filters( $query ) {

global $pagenow;
	
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['pickup_location_filter'] ) && $_GET['pickup_location_filter'] != '' && $_GET['post_type'] == 'shop_order' ) {

  // Create meta query array and add to WP_Query
  $meta_key_query = array(
    array(
      'key'     => 'Pickup Location',
      'value'   => esc_attr( $_GET['pickup_location_filter'] ),
    )
  );
  $query->set( 'meta_query', $meta_key_query );

}

}

add_action( 'pre_get_posts', 'apply_my_custom_product_filters' );

Read more here: Filtering WooCommerce Orders by Pickup Location


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: