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();