本文介绍了根据Woocommerce选择的付款方式,在结帐时更改“付款"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,有人知道如何根据所选的付款方式更改结帐时的付款"按钮吗?我找到了一些东西,但我不知道是否可以在function.php中将其转换为片段?谢谢你.
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? Thank you.
public function __construct() {
$this->id = 'ry_ecpay_atm';
$this->has_fields = false;
$this->order_button_text = __('Pay via ATM', RY_WT::$textdomain);
$this->method_title = __('ECPay ATM', RY_WT::$textdomain);
$this->method_description = '';
推荐答案
这可以通过以下代码(您将在其中设置您的付款网关ID和相应的所需按钮文字)完成:
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;
}
代码进入活动的子主题(或主题)的function.php文件中.经过测试并可以工作.
Code goes in function.php file of your active child theme (or theme). Tested and works.
这篇关于根据Woocommerce选择的付款方式,在结帐时更改“付款"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!