In my functions.php file, I need to be able to set the shipping method (there's only one for this site) and then return the cost for shipping (there are a few costs set).
I can see the flat_rate
shipping using this:
foreach (WC()->shipping->get_shipping_methods() as $key => $value){
echo $key;
}
So it's definitely there.
Basically, I want the shipping cost returned in from an API call. I have the API routed and all that working. It calls this function that I picked up somewhere, don't recall currently:
function pc_get_shipping(){
$ret = array();
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
$rate_label = $rate->label; // The shipping method label name
$rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
// The taxes cost
$rate_taxes = 0;
foreach ($rate->taxes as $rate_tax)
$rate_taxes += floatval($rate_tax);
// The cost including tax
$rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;
$ret[] = array('label' => $rate_label, 'total' => WC()->cart->get_cart_shipping_total());
}
}
return $ret;
}
But that gives me just an empty array, probably because WC()->session->get('shipping_for_package_0')['rates']
evaluates to an empty array.
TL:DR
Guest customer shipping info is saved via
WC()->customer->set_shipping_address_1(wc_clean($value));
(etc for all values)Guest customer shipping info is correctly returned using
WC()->customer->get_shipping()
, so I believe it is being set correctly.The shipping method
flat_rate
is available viaWC()->shipping->get_shipping_methods()
.How do I set the shipping method for the current order in functions.php, in a method that will be called via REST API.
How do I get the calculated shipping cost for the current order in functions.php, in a method that will be called via REST API.