Reputation: 14283
I'm trying to select the chosen_shipping_method price in woocommerce using this code:
$packages = WC()->shipping->get_packages();
foreach ($packages as $i => $package) {
$chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';
}
echo $chosen_method;
the code works, but it prints the ID, and i cannot figure out how to make it print out the price.
Here's package structure:
What i get with my code is table_rate_shipping_shipping_self_install
, but what i need is the cost for the selected element, and not the id.
i tried to change the code like this:
foreach ($packages as $i => $package['rates']) {
$chosen_method = isset(WC()->session->chosen_shipping_methods[$i]) ? WC()->session->chosen_shipping_methods[$i] : '';
}
But it printed out the id, same as before.
Any ideas? sorry i'm a bit new to php. Thanks in advance
Upvotes: 0
Views: 7262
Reputation: 31
If yo need to get price of shipping method that was chosen by user and stored in session, you should try this:
$chosen_shipping_method_price = WC()->session->get('cart_totals')['shipping_total'];
Upvotes: 3
Reputation: 3741
Did you try this inside your foreach loop?
$rate = $package['rates'][$chosen_method]->cost;
Upvotes: 5