I am trying to get the “Order Total” in a function with this code. But not working. The total is not printed at all.
What am I missing here?
add_action('woocommerce_checkout_before_order_review' , 'add_in_order_review'); function add_in_order_review(){ $order = wc_get_order( $order_id ); if ( $order ) { $totalOrder = $order->get_formatted_order_total( ); echo $totalOrder; } }
Advertisement
Answer
$order_id
is not defined, so you cannot access the $order
this way either.
The $order
object is also not yet created against the $cart
object.
So you get:
function action_woocommerce_checkout_before_order_review () { // Get cart total $cart_total = WC()->cart->get_cart_contents_total(); echo 'CT = ' . $cart_total; } add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );
The orders that you can access are orders that have already been created in previous orders
Like:
function action_woocommerce_checkout_before_order_review () { // An order id from a previous order $order_id = 1966; // Get order object $order = wc_get_order( $order_id ); // Is a WC order if ( is_a( $order, 'WC_Order') ) { $total_order = $order->get_formatted_order_total(); echo 'TO = ' . $total_order; } } add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );