如何防止Woocommerce在Checkout页面上选择默认付款方式? [英] How To Prevent Woocommerce from selecting default payment method on Checkout page?
问题描述
在结帐"页面上显示付款方式,默认情况下自动选择第一个付款方式.我需要阻止选择,因此WC最初没有选择任何付款方式.
On Checkout page payment methods are presented and the first one is selected by default and automatically. I need to prevent the selection so no payment method is initially selected by WC.
到目前为止,我尝试了2件事:
I tried 2 things so far:
-
Chrome控制台中的
-
jQuery:
jQuery from Chrome console:
jQuery('.payment_methods input.input-radio').prop('checked',false);
jQuery( '.payment_methods input.input-radio' ).prop('checked', false);
结果:
[<input id="payment_method_paypal" type="radio" class="input-radio" name="payment_method" value="paypal" data-order_button_text="Proceed to PayPal" checked="checked">,
<input id="payment_method_accountfunds" type="radio" class="input-radio" name="payment_method" value="accountfunds" data-order_button_text>]
-
从Payment-method.php Woocommerce模板文件中删除代码:
Remove the code from payment-method.php Woocommerce template file:
checked($ gateway-> chosen,false);
checked( $gateway->chosen, false );
都不起作用.怎么做?请提供任何摘要或建议吗?
Neither is working. How to do it? Any snippet or suggestion for that, please?
也尝试过这个:
function wpchris_filter_gateways( $gateways ){
global $woocommerce;
foreach ($gateways as $gateway) {
$gateway->chosen = 0;
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wpchris_filter_gateways', 1);
推荐答案
好的,可以使用了.方法如下:
OK, got it working. Here is how:
- 从以下位置复制JavaScript文件:
/wp-content/plugins/woocommerce/assets/js/frontend/checkout.js
进入:
/wp-content/themes/Your-Theme/woocommerce/js/checkout.js
-
打开该新创建的文件并搜索以下代码:
Open that newly created file and search for the following code:
if ($('.woocommerce-checkout').find('input[name=payment_method]:checked').size() === 0) {
$('.woocommerce-checkout').find('input[name=payment_method]:eq(0)').attr('checked', 'checked');
}
应该在298行附近.继续进行注释.
It Should be around line 298. Go ahead and comment it out.
-
将此添加到您的functions.php文件中:
Add this to your functions.php file:
function wpchris_override_woo_checkout_scripts() {
wp_deregister_script('wc-checkout');
wp_enqueue_script('wc-checkout', get_stylesheet_directory_uri() . '/woocommerce/js/checkout.js', array('jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n'), null, true);
}
add_action('wp_enqueue_scripts', 'wpchris_override_woo_checkout_scripts');
function wpchris_unselect_payment_method() {
echo "<script>jQuery( '.payment_methods input.input-radio' ).removeProp('checked');</script>";
}
add_action('woocommerce_review_order_before_submit','wpchris_unselect_payment_method' );
function wpchris_filter_gateways( $gateways ){
global $woocommerce;
foreach ($gateways as $gateway) {
$gateway->chosen = 0;
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wpchris_filter_gateways', 1);
现在,刷新结帐"页面时不应检查默认的付款方式.
Now, the default payment method should not get checked when you refresh the Checkout page.
这篇关于如何防止Woocommerce在Checkout页面上选择默认付款方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!