[英]WooCommerce - Deselect Default Payment Method
I am trying to get WooCommerce to not automatically select the first payment method on the checkout page.我试图让 WooCommerce 不自动选择结帐页面上的第一种付款方式。
This would force the customer to select a payment method on their own and neaten up the checkout page.这将迫使客户自行选择付款方式并整理结帐页面。 At present, with the first payment method automatically selected lots of payment information relating to that method is shown to the customer and pushes the other payment methods down the page.目前,使用第一种支付方式自动选择大量与该方式相关的支付信息显示给客户,并将其他支付方式推送到页面下方。 On mobile this is a problem as some think this is the only method of payment due to stacking.在移动设备上,这是一个问题,因为有些人认为这是堆叠的唯一支付方式。
The below JS works in removing a default payment method being selected.下面的 JS 用于删除选择的默认付款方式。
However, when I try to then select a payment method, it initially loads the gateway information but then disappears and the selection is removed.但是,当我尝试选择付款方式时,它最初加载网关信息,但随后消失并删除了选择。 I suspect AJAX is causing an issue here due to how this section reloads.由于此部分的重新加载方式,我怀疑 AJAX 在这里引起了问题。 This makes it impossible to select a payment method.这使得无法选择付款方式。
Could anyone help expand on this code to allow a gateway selection ?任何人都可以帮助扩展此代码以允许选择网关吗? Many Thanks非常感谢
jQuery(document).ready(function( $ ){
$( document ).on( 'updated_checkout', function() {
var $paymentCheckboxes = $( ".woocommerce-checkout-payment" ).find( '[name="payment_method"]');
$paymentCheckboxes.attr('checked', false);
$('.payment_box').hide();
});
});
Please test with the below code.请使用以下代码进行测试。 You need to use the 'updated_checkout' event to reset the payment gateway form after the "?wc-ajax=update_order_review" ajax call.在“?wc-ajax=update_order_review”ajax 调用之后,您需要使用“updated_checkout”事件来重置支付网关表单。
jQuery(document).ready(function ($) {
function deselectDefaultGateway() {
$('input[name="payment_method"]').each(function (index, item) {
$(item).attr('checked', false);
})
$('.payment_box').hide();
}
$(document.body).on('updated_checkout', deselectDefaultGateway);
$(document).on('click', '.wc_payment_method', function(e){
if (e.originalEvent !== undefined) {
$(document.body).off('updated_checkout', deselectDefaultGateway);
}
});
});
And, for your information, to prevent default selection on loading you can use code like below.而且,为了您的信息,为了防止加载时的默认选择,您可以使用如下代码。
add_action( 'woocommerce_before_template_part', 'custom_before_template_part', 10, 4 );
function custom_before_template_part($template_name, $template_path, $located, $args) {
if ( 'checkout/payment-method.php' == $template_name ) {
$gateway = $args['gateway'];
$gateway->chosen = false;
}
}
Woocommerce selects the first payment gateway as the default, but with the above 2 code blocks, you can completely change the behavior. Woocommerce 选择第一个支付网关作为默认,但是通过上面的 2 个代码块,您可以完全改变行为。 Hope this would help you.希望这会帮助你。
Try this code to select default method.
add_action( 'template_redirect', 'define_default_payment_gateway' );
function define_default_payment_gateway(){
if( is_checkout() && ! is_wc_endpoint_url() ) {
// HERE define the default payment gateway ID
$default_payment_id = 'stripe';
WC()->session->set( 'chosen_payment_method', $default_payment_id );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.