WooCommerce:按国家/地区代码获取送货方式

时间:2020-09-29 04:27:31

标签: php wordpress woocommerce hook-woocommerce woocommerce-rest-api

我正在尝试按请求中给出的国家/地区代码获取可用的送货方式,在上面的代码中我遇到了问题/冲突

可用位置可以是countrycontinent,因此我需要检查此国家/地区代码是否属于某个大陆。

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

第二个具有大洲/国家(世界其他地区)的区域,我对此没有任何问题,但到目前为止,我猜这是因为循环的尽头。 (因为我现在有两个区域)

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 个答案:

答案 0 :(得分:0)

答案如下:(添加 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;
        }

    )
);
相关问题