Reputation:
Hi I have this following code to get shipping methods from WooCommerce.
global $woocommerce;
$cart = wc()->cart->get_shipping_packages();
$sm = wc()->shipping->calculate_shipping_for_package($cart);
return $sm;
and the result will return me an array like this:
how do I foreach it to get the data from rate array? and the value I want are id,label and method_id?
print_r($sm); exit;
Array
(
[0] => Array
(
[contents] => Array
(
)
[contents_cost] => 0
[applied_coupons] => Array
(
)
[user] => Array
(
[ID] => 1
)
[destination] => Array
(
[country] => SG
[state] =>
[postcode] =>
[city] =>
[address] =>
[address_2] =>
)
)
[rates] => Array
(
[international_delivery] => WC_Shipping_Rate Object
(
[id] => international_delivery
[label] => International Flat Rate
[cost] => 10
[taxes] => Array
(
)
[method_id] => international_delivery
)
[table_rate-5 : 7] => WC_Shipping_Rate Object
(
[id] => table_rate-5 : 7
[label] => Registered Mail
[cost] => 0
[taxes] => Array
(
)
[method_id] => table_rate
)
[table_rate-5 : 8] => WC_Shipping_Rate Object
(
[id] => table_rate-5 : 8
[label] => Non-Registered Mail
[cost] => 0
[taxes] => Array
(
)
[method_id] => table_rate
)
[table_rate-5 : 9] => WC_Shipping_Rate Object
(
[id] => table_rate-5 : 9
[label] => Courier
[cost] => 0
[taxes] => Array
(
)
[method_id] => table_rate
)
)
)
Upvotes: 2
Views: 4616
Reputation: 3427
foreach ($sm['rates'] as $key => $value)
{
$method = array();
$method['label'] = $value->label;
$method['method'] = $value->method_id;
$method['id'] = $value->id;
$shippingMethods[] = $method;
}
return $shippingMethods;
Upvotes: 5
Reputation: 3005
It look like a JSON object, user json_decode() function to convert. Visit bellow link for more info http://php.net/manual/en/function.json-decode.php
Upvotes: 2