I've been trying for hours now and can't get this "basic" thing to work anyway.
I have a bunch of payment gateways available and I need to include their names (including order total amount) in the "Pay Now" button text.
Example: "Use Stripe to pay for order $49
"
I have a piece of code that is said to automatically update the checkout when changing gateways.Please, can anyone help?
add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10, 1 ); function order_button_text_based_on_gateway( $cart ) { // Make sure we get the payment gateway $payment_method = WC()->session->get( 'chosen_payment_method' ); // Based on different gateways, display different button text (order button) if ( $payment_method == ' bacs ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use WireTransfer' ); } elseif ( $payment_method == ' check ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use personal check' ); } elseif ( $payment_method == ' cod ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Cash on delivery' ); } elseif ( $payment_method == ' etco ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use EtCo' ); } else ( $payment_method == ' stripe ' ) { return sprintf( '%s %s', __('Place order and pay', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . 'Use Stripe' ); } }
Auto-update checkout script:
add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); function reload_checkout_based_on_gateway_change() { if ( is_checkout() && ! is_admin() ) { //End PHP and start SCRIPT ?>
There are a lot of errors in your code:
'cheque'
and'cheque'
are two different strings.So among all the if statements, none of the payment methods match.
else
does not support any conditional parameters.There are several ways to change the text of the checkout "Place Order" button:
Or you can also use the
order_button_text
attribute ofWC_Payment_Gateway
, as shown below:Place the code in your child theme’s functions.php file (or in a plugin). It has been tested and works fine.