0

I guess this is not really a duplicate, I am trying to calculate the shipping price the same way woocommerce does this whenever a customer places an order, the "related" questions seems to talk about setting a fixed price for the orders...

I'm trying to calculate the shipping price based on the country of the customer in a dynamically generated order (basically an order created by wc_create_order()), but it seems like using calculate_totals method doesn't do much.

I tried searching online but nothing usefull showed up, I tried using the calculate_shipping() method from WC_Abstract_Order but it didn't work, what could I do to calculate the right shipping price?

Is there a function somewhere that just returns the shipping price/rates for a shipping address?

Here's a snippet of what I tried (I omitted the part where I add the items)


    // Retrieving the shipping and billing address from the POST request
    $shipping = json_decode(stripslashes($_POST['shipping']), true);
    $products_ids = json_decode(stripslashes($_POST['products']), true);

    // Adding them to the order
    $order->set_address($shipping, 'shipping');

    if (isset($_POST['billing'])){
        $bill = json_decode(stripslashes($_POST['billing']), true);
        $bill['email'] = $shipping['email'];
    }
    else {
        $bill = $shipping;
    }

    $order->set_address($bill,'billing');

       ............

    // Calculating the order total based on the items inside the cart 
    // (the calculate_shipping doesn't really do much)
    $order->calculate_shipping();
    $order->calculate_totals();
double-beep
  • 5,031
  • 17
  • 33
  • 41
Maxpnl
  • 98
  • 2
  • 10
  • 1
    Of course, sorry about that, I added some comments and changed variables name, hope it's more clear now – Maxpnl May 10 '19 at 09:50
  • Please add a new answer if you found a question. Answers are unlocked, since your question was reopened. Good luck! – double-beep May 13 '19 at 20:28

1 Answers1

0

So, after struggling a little bit, I found a solution to my problem, not a clean one but still, it's not so bad. Here's the code!

if( class_exists( 'WC_Shipping_Zones' ) ) {
    $all_zones = WC_Shipping_Zones::get_zones();

    $country_code = "CN"; //Just testing with a random country code, this should be your input

    foreach ($all_zones as $key => $zona) {
        $total_zones = $zona['zone_locations'];
        // Getting all the zones and iterating them until you find one matching $country_code
        foreach ($total_zonesa as $cur_country_code) {
            if ($cur_country_code->code == $country_code){
                // If you find a match you iterate over shipping_methods, if you don't have more than one flat_rate you should just break this like I did, otherwise you would have to struggle a little bit more with this
                //The $flat_rate->cost is the thing you want to return
                foreach ($zona['shipping_methods'] as $key => $value) {
                    $instance_id = $key;
                    $flat_rate = new WC_Shipping_Flat_Rate($instance_id);
                    print_r($flat_rate->cost);
                    break;
                }
            }
        }
    }
}
return false;
Maxpnl
  • 98
  • 2
  • 10