WooCommerce:通过国家代码获取运输方式

WooCommerce: get a shipping methods by Country code

我正在尝试通过请求中给出的国家/地区代码获取可用的送货方式,在上面的代码中我遇到了 issue/conflict

可用位置可以是 countrycontinent,所以我需要检查这个国家代码是否属于某个大陆。

问题出在第一个区域,我得到了所有区域中的所有方法,而不仅仅是第一个区域(回调 return)。

具有 continents/countries(世界其他地区)的第二个区域我对此没有任何问题,但据我猜测这是因为它已结束循环。 (因为我现在有两个区域)

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;
            }

        )
    );
});

答案如下:(将 break 与要停止的数组编号相加)

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;
        }

    )
);