In my shop, customers can buy the same product from several locations. Each location has its own shop_manager, so I want every shop_manager can only see the orders where the selected variation matches their location (e.g. "shop_manager_min", from Minnesota, can only see orders where variation "min" has been selected; "shop_manager_tex", from Texas, can only see orders with "tex" variation and so on).
To do so, I tried the next code:
add_filter( 'pre_get_posts', 'orders_by_location' );
function orders_by_location( $query ) {
//check if user role is shop_manager to continue
if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop_manager'){
//gets the 3 character string code for the shop manager location
$location = substr(wp_get_current_user()->user_login, -3);
//query for all the orders where the variation matches the shop manager location
$query->set( 'meta_query', array(
array(
'key' => 'attribute_pa_location',
'value' => $location,
'compare' => '='
) ) );
return $query; }}
However, I got nothing back from this query, no orders are shown. What I am doing wrong?
I have tried the next query to order orders by location, and it works just perfectly:
add_filter( 'pre_get_posts', 'orders_by_location' );
function orders_by_location( $query ) {
if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop_manager'){
$query->set('orderby', 'attribute_pa_location' );
$query->set('order', 'DESC' );
return $query; }}
With the previous, I see attribute_pa_location exists and it is accessible by the query. I also echoed the attribute_pa_location value and the value in $location, and both are the exactly same!!! However, the query is not giving anything back and no orders are shown in the shop_manager admin order page.
I did the same first query using _customer_user
instead of attribute_pa_sede
and worked perfectly. If the query procedure is fine and the meta key is also fine, why is not working when I use them together???