Search code examples
phpwordpresswoocommercehook-woocommerceorders

WooCommerce - Add $order_total to place order button


I want to add $order->get_total(); to my woocommerce place order button, on the checkout page. So I just want it to display the total as an string.

This is what I have in my functions.php, which is returning a blank.

add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' ); 

function woo_custom_order_button_text() {
    return __( $order->get_total(), 'woocommerce' ); 
}

I have tried this as well:

function woo_custom_order_button_text() {
    return __( $order_total, 'woocommerce' ); 
}

Both snippets returns a blank, nothing.

How can this be done? Thanks.


Solution

  • You have to use WC() which is an alias of global $woocommerce to access WooCommerce related data, and to access cart information you have to use WC()->cart.

    This code should work for you.

    add_filter('woocommerce_order_button_text', 'woo_custom_order_button_text');
    
    function woo_custom_order_button_text()
    {
        $cart_total = WC()->cart->total;    
        return __('Your text ' . $cart_total, 'woocommerce');
    }
    

    Hope this helps!