WooCommerce Получить все способ доставки Расчет API - PullRequest
0 голосов
/ 25 апреля 2018

Я пытаюсь создать функции API для мобильных устройств, чтобы получить способы доставки из WordPress WooCommerce.Это мой код:

    public function get_shipping($request){
        $params = (array) json_decode(file_get_contents('php://input'), TRUE);
        $state =  $params['state'];
        $country =  $params['country'];
        global $woocommerce;
        $woocommerce->cart->empty_cart();
        foreach ($params['line_items'] as $item) {
           if((int) $item["variation_id"] > 0 ){
               $woocommerce->cart->add_to_cart(
                   (int)  $item["id"],
                   (int) $item["quantity"],
                   $item["variation_id"]);
           } else {
               $woocommerce->cart->add_to_cart(
                   (int)  $item["id"],
                   (int) $item["quantity"]);
           }
        }

        $package =  $woocommerce->cart->get_shipping_packages();
        $delivery_zones = (Array) WC_Shipping_Zones::get_zones();
        $new_delivery_zones = Array();
        foreach ($delivery_zones as $zone) {
            $z_object =  (Array) $zone;
            $new_shippings = Array();
            foreach ($z_object["shipping_methods"] as $shipping) {
                $s_item = Array();
                $objects = (Array) $shipping;
                foreach ($objects as $key => $value) {
                    if($key != "instance_form_fields") {
                        $s_item[$key] = $value;
                    }else{
                        $f_item = Array();
                        $new_form_fields = Array();
                        $form_fields = (Array) $value;
                        foreach ($form_fields as $keyi => $valuei) {
                            $valuei["name"] = $keyi;
                            $f_item[$keyi] = $valuei;
                            array_push($new_form_fields, $valuei);
                        }
                        $s_item["instance_form_fields"] = $new_form_fields;
                    }
                }
                array_push($new_shippings, $s_item);

            }
            $z_object["shipping_methods"] = null;
            $z_object["shipping_methods"] = $new_shippings;
            array_push($new_delivery_zones, $z_object);

        }

        $correct_zone = null;
        foreach ($new_delivery_zones as $zone) {
            foreach ($zone["zone_locations"] as $location) {
                 $splits = explode(":",$location-> code);
                 if(count($splits) > 1){
                     $country = $splits[0];
                     $code = $splits[1];
                     if($country == $country && $code == $state ){
                         $correct_zone = $zone;
                     }
                 }
            }
        }
        if($correct_zone == null)
            return $correct_zone;

        $package['destination']['country'] = $country;
        $package['destination']['state'] = (int) $state;
        $package['destination']['country'] = $country;
        $new_shipping_method = Array();
        foreach ($correct_zone["shipping_methods"] as $shipping_method) {
            if (class_exists($shipping_method["id"])) {
                $class = new $shipping_method["id"]();
                $class->calculate_shipping_for_package($package);

                foreach ($class->rates as $rate) {
                    array_push($shipping_method["rates"], Array(
                        "cost" => $rate->cost,
                        "label" => $rate->label,
                        "method_id" => $rate->method_id
                    ));
                }
//                error_log(print_r($class->rates, true));
                array_push($new_shipping_method, $shipping_method);
            }else{
                array_push($new_shipping_method, $shipping_method);
            }
        }
        $correct_zone["shipping_methods"] = $new_shipping_method;

        return $correct_zone;
    }

, и я читаю эту запись

НО я не понял, в чем проблема, потому что она не работает должным образом иего расчетные цены не совпадают с пост-плагинами.

Пожалуйста, пожалуйста, пожалуйста, дайте мне правильный код большое спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...