5

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!

AndiM
  • 2,196
  • 2
  • 21
  • 38

0 Answers0