Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change "Place order" button text to include Order Total

Tags:

woocommerce

The idea here is to include the price (example: $40) with the button text, making it like this: "Place Order & Pay $40".

That is the expected outcome but the code I'm using (with or without wc_price()) only generates a span tag on the button.

I know that I can just add the currency symbol myself into the button text, but the idea is to make it global.

My code so far:

add_filter('woocommerce_order_button_text', 'place_order_button_with_order_total');

function place_order_button_with_order_total(){     
  $order_value = wc_price(WC()->cart->total);
        
  return __('Place Order & Pay '.$order_value., 'woocommerce');
}

How do I change the code to include the price in the button text?


1 Answers

You can remove the HTML markup that you get when using wc_price (or in my example get_total()) with the strip_tags() function.

Also you shouldn't add variables to translatable strings. If you want to make a combination of a translatable string and a variable value you can make use of the sprintf() function.

The following code should do the trick:

add_filter('woocommerce_order_button_text', 'place_order_button_with_order_total');
function place_order_button_with_order_total(){
    return sprintf( '%s %s', __( 'Place Order & Pay', 'custom-order-button' ), strip_tags( WC()->cart->get_total() ) );
}
like image 65
Terminator-Barbapapa Avatar answered Sep 20 '25 11:09

Terminator-Barbapapa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!