duanji7182 2019-06-04 05:24
浏览 107

WooCommerce通过REST API计算运费

My implementation in app is just like my WooCommerce website. I want to achieve following points for calculating shipping:

  1. Check if address is required or not for calculating shipping address?
  2. If address is entered by user, how to check if this address falling between shipping method crieteria? Give error to user if entered address is not valid as per shipping method.
  3. Once user enter valid address, calculate cost of all shipping methods.
  4. Show all shipping methods to user in app with its cost with default method selected.
  5. When user switch between shipping methods, it should save and sync with website too and get cart total as per selected method.

Till now what I achieve is point 4. I am able to get all shipping methods calculated via website but if not calculated via website then it returns me null there.

Here is my API code:

function register_get_cart_shipping() {

    register_rest_route(
        'custom-plugin', '/getCartShipping/',
        array(
            'methods'  => ['GET'],
            'callback' => 'getCartShipping',
        )
    );

        function getCartShipping() {
            $chosen_methods = WC()->session->get('chosen_shipping_methods');
            $count = 0;
            foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){

                $data[$count]->rate_id        = $rate->id;
                $data[$count]->method_id      = $rate->method_id;
                $data[$count]->instance_id    = $rate->instance_id;
                $data[$count]->label          = $rate->label;
                $data[$count]->cost           = $rate->cost;
                $data[$count]->taxes          = $rate->taxes;
                $data[$count]->chosen         = $chosen_methods['rate_id'];

                if($chosen_methods['rate_id'] == $rate->id ){
                    $data[$count]->isSelected  = true;
                } else {
                    $data[$count]->isSelected  = false;
                }
                $count++;
            }
            return $data;
        }
}

Also for point 5, when user switch between shipping methods, I am using this code but it doesn't update selected shipping method on website. Here is the code:

function updateCartShipping($request) {
            $rate_id["rate_id"] = "table_rate:10:4";
            // $rate_id["rate_id"] = "table_rate:9:3";
            // $rate_id["rate_id"] = $request['shippingID'];
            WC()->session->set('chosen_shipping_methods', $rate_id);
            return "OK";
}

I also don't know which method id to set as a shipping method. It seems mysterious to me.

Any help will be appreciated. Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP