Hi Anyone knows how to change Pay button on checkout based on chosen payment method? I found something but I don\'t know if I could turn it into a snippet in function.php? T
I think this is an easier solution:
add_filter('woocommerce_available_payment_gateways', 'change_barion_label');
function change_barion_label($gateways) {
if($gateways['ry_ecpay_atm']) {
$gateways['ry_ecpay_atm']->order_button_text = 'new label';
}
return $gateways;
}
WooCommerce runs this filter when loading the payment gateways, so it should work site-wide.
This can be done with the following code (where you will set your payment gateway IDs and the corresponding desired button text):
add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
function custom_order_button_text( $order_button_text ) {
$default = __( 'Place order', 'woocommerce' ); // If needed
// Get the chosen payment gateway (dynamically)
$chosen_payment_method = WC()->session->get('chosen_payment_method');
// Set your payment gateways IDs in EACH "IF" statement
if( $chosen_payment_method == 'bacs'){
// HERE set your custom button text
$order_button_text = __( 'Bank wire payment', 'woocommerce' );
} elseif( $chosen_payment_method == 'ry_ecpay_atm'){
// HERE set your custom button text
$order_button_text = __( 'Place order via ECPay', 'woocommerce' );
}
// jQuery code: Make dynamic text button "on change" event ?>
<script type="text/javascript">
(function($){
$('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
var t = { updateTimer: !1, dirtyInput: !1,
reset_update_checkout_timer: function() {
clearTimeout(t.updateTimer)
}, trigger_update_checkout: function() {
t.reset_update_checkout_timer(), t.dirtyInput = !1,
$(document.body).trigger("update_checkout")
}
};
t.trigger_update_checkout();
});
})(jQuery);
</script><?php
return $order_button_text;
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
function custom_order_button_text( $order_button_text ) {
$default = __( 'Place order', 'woocommerce' ); // If needed
// Get the chosen payment gateway (dynamically)
$chosen_payment_method = WC()->session->get('chosen_payment_method');
## --- For TESTING raw output on the chosen gateway ID --- ##
// echo '<pre>' . $chosen_payment_method . '</pre>'; // <=== uncomment for testing
// Set your payment gateways IDs in EACH "IF" statement
if( $chosen_payment_method == 'bacs'){
// HERE set your custom button text
$order_button_text = __( 'Bank wire payment', 'woocommerce' );
} elseif( $chosen_payment_method == 'ecpay_shipping_pay'){
// HERE set your custom button text
$order_button_text = __( 'Place order via Market', 'woocommerce' );
} elseif( $chosen_payment_method == 'ecpay'){
// HERE set your custom button text
$order_button_text = __( 'Place order via ATM/Credit Card', 'woocommerce' );
}
// jQuery code: Make dynamic text button "on change" event ?>
<script type="text/javascript">
(function($){
$('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
var t = { updateTimer: !1, dirtyInput: !1,
reset_update_checkout_timer: function() {
clearTimeout(t.updateTimer)
}, trigger_update_checkout: function() {
t.reset_update_checkout_timer(), t.dirtyInput = !1,
$(document.body).trigger("update_checkout")
}
};
t.trigger_update_checkout();
});
})(jQuery);
</script><?php
return $order_button_text;
}
and this is the payment in that dropdown.
'ecpay_payment_methods' => array(
'title' => __( 'Payment Method', 'ecpay' ),
'type' => 'multiselect',
'description' => __( 'Press CTRL and the right button on the mouse to select multi payments.', 'ecpay' ),
'options' => array(
'Credit' => $this->get_payment_desc('Credit'),
'Credit_3' => $this->get_payment_desc('Credit_3'),
'Credit_6' => $this->get_payment_desc('Credit_6'),
'Credit_12' => $this->get_payment_desc('Credit_12'),
'Credit_18' => $this->get_payment_desc('Credit_18'),
'Credit_24' => $this->get_payment_desc('Credit_24'),
'WebATM' => $this->get_payment_desc('WebATM'),
'ATM' => $this->get_payment_desc('ATM'),
'CVS' => $this->get_payment_desc('CVS'),
'BARCODE' => $this->get_payment_desc('BARCODE'),
'ApplePay' => $this->get_payment_desc('ApplePay')
),