0

I would like to only show wholesale_customers orders from Germany and Austrian (DE / AT) to a certain shop manager with the ID: 136. I have found some code here. I have found and made the following, but its not working?

    function before_checkout_create_order($order, $data) {

        $country = $order->billing_country;
        $store_manager_id = '136';

        $german_region = ['DE', 'AT'];

        if (in_array($country, $german_region)) {
            $store_manager_id = 136;
        }

        $order->update_meta_data('_store_manager_id', $store_manager_id);
    }
    add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
Martijn
  • 17
  • 4

1 Answers1

0

UPDATE: Simplified solution without custom post_meta (works with previously created orders as well).

You can use pre_get_posts action directly to alter meta_query based on order billing country codes. If you would like to use this solution, you can safely remove the before_checkout_create_order function as it is no longer needed.

function store_manager_orders_query( $query ) {
    global $pagenow, $typenow;
    $store_manager_id = 136;
    $store_manager_countries = array('DE', 'AT');

    if( !$query->is_main_query() ) {
        return;
    }

    if( get_current_user_id() === $store_manager_id && $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $typenow ){

        // Get and alter meta query
        $meta_query = (array)$query->get('meta_query');
        $meta_query[] = array(
            'key'     => '_billing_country',
            'value'   => $store_manager_countries,
            'compare' => 'IN',
        );

        // Set altered meta query
        $query->set('meta_query',$meta_query);
    }
}
add_action( 'pre_get_posts', 'store_manager_orders_query', 20 );

OLD SOLUTION: (uses redundant custom order meta from your original question)

Your code basically adds _store_manager_id meta to every created order, because your if condition does nothing (it only re-assign the store manager ID to the same value as specified before), and then you update the order meta of every order, without any conditional logic. You should run update_meta_data function inside that if condition:

function before_checkout_create_order( $order, $data ) {
    
    $country = $order->billing_country;
    $german_region = ['DE', 'AT'];

    if( in_array( $country, $german_region ) ) {
        $store_manager_id = 136;
        $order->update_meta_data('_store_manager_id', $store_manager_id);
    }
}
add_action( 'woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2 );

Now only desired (DE, AT) orders have the _store_manager_id meta set to 136. Now you need to limit queried orders for that store manager:

function store_manager_orders_query( $query ) {
    global $pagenow, $typenow;
    $store_manager_id = 136;

    if( !$query->is_main_query() ) {
        return;
    }

    if( get_current_user_id() === $store_manager_id && $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $typenow ){

        // Get and alter meta query
        $meta_query = (array)$query->get('meta_query');
        $meta_query[] = array(
            'key'     => '_store_manager_id',
            'value'   => $store_manager_id,
            'compare' => '=',
        );

        // Set altered meta query
        $query->set('meta_query',$meta_query);
    }
}
add_action( 'pre_get_posts', 'store_manager_orders_query', 20 );

Tested and works. Both portions of the code go in functions.php of your active theme or child-theme.

Please note that your current code probably assigned this meta value to every created order (not only german orders), so there are probably non-german orders with the _store_manager_id meta set to 136 already. You should clean up these database records, change your meta_key, or just ignore past orders.

TIP: You can make this code more versatile if there will be more restricted locations for specific store managers in the future (e.g. enable/disable restriction on user level and then automatically show orders with _store_manager_id meta with the same value as get_current_user_id(). Whatever suits your scenario.

Kristián Filo
  • 827
  • 2
  • 8
  • 25
  • @Martijn I understand. The reason probably is that you currently have no orders with that custom meta set yet. Please **check my updated simplified answer** above and let me know if it works. – Kristián Filo Jul 27 '22 at 12:23
  • @Martijn You are welcome. However it really should show all orders that have the billing country set to `DE` or `AT`. Are you 100% sure there are old orders with those billing countries? I've just tested in on my store with thousands of orders and it works well. – Kristián Filo Jul 27 '22 at 13:45