WooCommerce: get a shipping methods by Country code

1.4k Views Asked by At

I'm trying to get the shipping available methods by country code that giving in the request, in the above code I'm facing an issue/conflict

That the available locations can be both a country or a continent so I need to check if this country code is a part of some continent.

the problem is with the first zone I get all methods in all zones not just within the first ( the callback return ).

The second zone which has the continents/countries ( rest of the world ) I get no issues with it but as far I guess that's because its the end of the loop. ( as I have two zones for now )

add_action("rest_api_init", function () {
    register_rest_route(
        "test-api/v1",
        "shipping-cost",
        array(
            'callback' => function ($req) {
                $country_code = $req->get_param('country_code');
                $quantity = $req->get_param('quantity');

                $shipping_cost = 0;
                $methodes = [];


                if (class_exists('WC_Shipping_Zones')) {
                    $all_zones = WC_Shipping_Zones::get_zones();


                    if (!empty($all_zones)) {
                        foreach ($all_zones as $zone) {
                            if (!empty($zone['zone_locations'])) {
                                foreach ($zone['zone_locations'] as $location) {

                                    $wc_contries = new WC_Countries();
                                    $continent_code = $wc_contries->get_continent_code_for_country($country_code);


                                    if ($country_code === $location->code || $continent_code === $location->code) {
                                        if (!empty($zone['shipping_methods'])) {
                                            $shipping_method_ctrl = new WC_REST_Shipping_Zone_Methods_Controller();

                                            foreach ($zone['shipping_methods'] as $flat_rate) {

                                                $shipping_method = $shipping_method_ctrl->prepare_item_for_response($flat_rate, $req);
                                                $methodes[] = (object) $shipping_method;

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
                return $methodes;
            }

        )
    );
});
1

There are 1 best solutions below

0
On BEST ANSWER

Here's the answer: ( adding break with the number of the array want it to be stop )

Register_rest_route(
    "kefan-api/v1",
    "shipping-cost",
    array(
        'callback' => function ($req) {
            $country_code = $req->get_param('country_code');
            $methodes = [];


            if (class_exists('WC_Shipping_Zones')) {
                $all_zones = WC_Shipping_Zones::get_zones();


                if (!empty($all_zones)) {
                    foreach ($all_zones as $zone) {
                        if (!empty($zone['zone_locations'])) {
                            foreach ($zone['zone_locations'] as $location) {

                                $wc_contries = new WC_Countries();
                                $continent_code = $wc_contries->get_continent_code_for_country($country_code);


                                if ($country_code === $location->code || $continent_code === $location->code) {
                                    if (!empty($zone['shipping_methods'])) {
                                        $shipping_method_ctrl = new WC_REST_Shipping_Zone_Methods_Controller();

                                        foreach ($zone['shipping_methods'] as $flat_rate) {

                                            $shipping_method = $shipping_method_ctrl->prepare_item_for_response($flat_rate, $req);
                                            $methodes[] = (object) $shipping_method;

                                        }
                                        break 2;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return $methodes;
        }

    )
);