skip to Main Content

I am using a Country Based Restriction WooCommerce plugin that users can order only by their own country. I have 3 physical store branches in 3 countries, and now I created 3 Shop Manager roles 1 per country, and i want them to restrict on the order page only they can view the customer orders by their specific assigned country.

I found a similar question from here but still no answer Show woocommerce orders by country

2

Answers


  1. Chosen as BEST ANSWER

    I made an experiment of this but I don't have any idea what I'm doing, I know this is wrong metas etc. hope you can help me to do this.

    function view_order($order, $data, $view) {
    
            $country = $order->shipping_country;
            $shop_manager_id = '';
    
            $general_santos = ['GES'];
            $koronadal = ['KOR'];
            $polomolok = ['POL'];
    
            if (in_array($country, $general_santos)) {
                // Manually assigning the _store_manager_id using the user id, yours will differ
                $shop_manager_id = 5;
            } else if (in_array($country, $koronadal)) {
                $shop_manager_id = 3;
            } else if (in_array($country, $polomolok)) {
                $shop_manager_id = 2;
            } else {
                $shop_manager_id = 5;
            }
    
            $order->update_meta_data('_store_manager_id', $shop_manager_id);
        }
        add_action('woocommerce_view_order', 'view_order', 20, 2);
    

  2. If you want to query orders by country. Here is a sample code.

    // If you want to get orders from America.
    $args = array(
        'billing_country' => 'US',
    );
    $orders = wc_get_orders( $args );
    

    Refer to this doc.

    https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#address-and-name

    And for this question,

    I have 3 physical store branches in 3 countries, and now I created 3
    Shop Manager roles 1 per country, and i want them to restrict on the
    order page only they can view the customer orders by their specific
    assigned country.

    I suggest creating a custom shortcode that would query the Orders record(use the code provided above) using the current country assigned to the current logged in Shop Manager (I suggest adding a user meta for the Shop Manager user where you can assign country code – e.g PH , US ) and list

    Heres a snippet code.

    $currentUserId = get_current_user_id();
    $currentShopManagerCountryCode = get_user_meta($currentUserId ,"assigned_country_code",true);
    $args = array(
        'billing_country' => $currentShopManagerCountryCode,
    );
    $orders = wc_get_orders( $args );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search