I've been trying to get the latest shipping cost information within the action hook 'woocommerce_checkout_update_order_review' by doing WC()->cart->get_shipping_total() but it seemingly contains the previous shipping total value and not the current shipping total value. The shipping total value only seems to be the "current" value when the user removes a field's information and re-inputs it back. I need the shipping total for a calculation I'm going to do later.
I've tried multiple different ways to get the latest shipping total information:
function wp_kama_woocommerce_checkout_update_order_review_action( $post_data )
{
wc_add_notice(WC()->cart->get_shipping_total(), 'notice');
mail("[email protected]", "shipping total", WC()->cart->get_shipping_total() );
}
add_action( 'woocommerce_checkout_update_order_review', 'wp_kama_woocommerce_checkout_update_order_review_action' );
function wp_kama_woocommerce_checkout_update_order_review_action( $post_data )
{
wc_add_notice(print_r($_POST, true), 'notice');
mail("[email protected]", "shipping total", print_r($_POST, true) );
}
add_action( 'woocommerce_checkout_update_order_review', 'wp_kama_woocommerce_checkout_update_order_review_action' );
function wp_kama_woocommerce_checkout_update_order_review_action( $post_data )
{
parse_str( $post_data, $post_data );
wc_add_notice($post_data, 'notice');
mail("[email protected]", "shipping total", print_r( $post_data, true ) );
}
add_action( 'woocommerce_checkout_update_order_review', 'wp_kama_woocommerce_checkout_update_order_review_action' );
This stackoverflow post had a similar experience but they were able to access their data via the parameter variable that the action hook 'woocommerce_checkout_update_order_review' provides but the shipping method within that variable doesn't seem to be the current value as previously stated.
Located in WC_Ajax update_order_review()
, woocommerce_checkout_update_order_review
hook is triggered before the chosen shipping method is set and before totals calculations, that's why you get the previous shipping total.
Instead, you could use one of the following action hooks:
woocommerce_review_order_before_shipping
woocommerce_review_order_after_shipping
woocommerce_review_order_before_order_total
woocommerce_review_order_after_order_total
So your code will be for example:
add_action( 'woocommerce_review_order_after_shipping', 'action_wc_review_order_after_shipping' );
function action_wc_review_order_after_shipping() {
wc_add_notice( 'Shipping total: ' . WC()->cart->get_shipping_total(), 'notice');
}