在结帐页面上显示付款方式,默认情况下会自动选择第一个。我需要阻止选择,所以 WC 最初没有选择付款方式。
到目前为止我尝试了两件事:
来自 Chrome 控制台的 jQuery:
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 模板文件中删除代码:
checked( $gateway->chosen, false );
两者都不工作。怎么做?请问有什么片段或建议吗?
编辑:
也试过这个:
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);
好的,开始工作了。方法如下:
/wp-content/plugins/woocommerce/assets/js/frontend/checkout.js
进入:
/wp-content/themes/Your-Theme/woocommerce/js/checkout.js
打开新创建的文件并搜索以下代码:
if ($('.woocommerce-checkout').find('input[name=payment_method]:checked').size() === 0) {
$('.woocommerce-checkout').find('input[name=payment_method]:eq(0)').attr('checked', 'checked');
}
应该在298行左右,继续注释掉。
将此添加到您的 functions.php 文件中:
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);
现在,当您刷新结账页面时,默认支付方式不应该被选中。
我有同样的问题,我是这样解决的:
用我的孩子主题 checkout-override-js 覆盖 checkout-js 文件。
然后注释掉下面的代码:
线:48:
this.init_payment_methods();
init_payment_methods:功能(){
var $payment_methods = $( '.woocommerce-checkout' ).find( 'input[name="payment_method"]' );
// If there is one method, we can hide the radio input
if ( 1 === $payment_methods.size() ) {
$payment_methods.eq(0).hide();
}
// If there are none selected, select the first.
if ( 0 === $payment_methods.filter( ':checked' ).size() ) {
$payment_methods.eq(0).attr( 'checked', 'checked' );
}
// Trigger click event for selected method
$payment_methods.filter( ':checked' ).eq(0).trigger( 'click' );
},
线:113:
wc_checkout_form.init_payment_methods();
这将删除默认的付款方式选择。您可以使用这些代码来满足您的要求。
没有通过过滤器实现此目的的好方法,这不适用于最新版本的 woocommerce 4.x:
//DEPRECATED. Will not work with woocommerce 4.x and above
add_filter( 'pre_option_woocommerce_default_gateway' . '__return_false', 99 );
不像其他答案所建议的那样覆盖 Woocommerce 核心 JS 文件,而是这样做:
var $paymentCheckboxes = $( ".woocommerce-checkout-payment" ).find( '[name="payment_method"]');
$paymentCheckboxes.attr('checked', false);
补充@Tosh 的答案,但添加了 javascript 回调,以便每次通过 ajax 更新结帐时它都会运行。
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();
});
});
测试和工作。
我用 jQuery 解决了这个问题。我所做的就是编写一个在页面加载后由滚动手势触发的函数。确实有效,任何人都可以将以下代码添加到主题文件中的
footer.php
文件的底部。另外,不要忘记根据您的需要编辑payment_method_name
。
jQuery(document).ready(function($){
$(window).scroll(function() {
if ($(this).scrollTop() > 0) {
if (!this.done) {
this.done = true;
var $paymentCheckboxes = $('.woocommerce-checkout-payment').find('[name="PAYMENT_METHOD_NAME_HERE"]');
$paymentCheckboxes.removeAttr('checked');
$('.payment_box').hide();
$('#PAYMENT_METHOD_NAME_HERE').prop("checked", false);
}
}
});
});
这些解决方案都不适合我。
我的解决方案是: 让 WooCommerce 表现得像它想要的那样 我在列表顶部添加了一个付款选项(在本例中为 COD)。 使用 CSS,我隐藏了整个
<li>
然后我在“woocommerce_after_checkout_validation”中添加了验证。 如果用户选择了这个,我只是简单地返回了一个“没有选择付款”的错误
似乎工作得很好。
$(document).on('updated_checkout',function() {
$('input[type=radio][name=payment_method]').change(function() {
$('body').addClass('payment_selection_made');
});
if (!$('body').hasClass('payment_selection_made')) {
$('.woocommerce-checkout-payment input[name="payment_method"]').attr('checked', false);
$('.woocommerce-checkout-payment .payment_box').hide();
}
});
我使用了@Viktor Borítás 的回答并在 WooCommerce 6.0.0 中运行良好。 为了覆盖 checkout.min.js 并使其独立于更新,我在子主题 functions.php 文件中使用了下面的函数。
//override WooCommerce Frontend Script (checkout.min.js)
add_action('wp_enqueue_scripts', 'override_woo_frontend_scripts');
function override_woo_frontend_scripts() {
wp_deregister_script('wc-checkout');
wp_enqueue_script('wc-checkout', get_stylesheet_directory_uri() . '/woocommerce/js/checkout.min.js', array('jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n'), null, true);
}
我需要更改结帐页面上的 Woocommerce 默认付款方式(我没有尝试过任何其他方法!)所以 没有 评论此代码:
if ( 0 === $payment_methods.filter( ':checked' ).length ) {
$payment_methods.eq(0).prop( 'checked', true );
相反,我将 payment_methods 选择的值更改为其他值。
也许这样的拐杖会帮助某人。在文件
payment-method.php
中添加
<input type="radio" class="input-radio" name="payment_method" style="display:none;" checked>
哪个答案最好请建议我
在
payment-method.php
中我简单地注释掉了这部分(WooCommerce 4.5.2 的第 23 行)
<?php //checked( $gateway->chosen, true ); ?>
(如果您有子主题,请将修改后的文件放入
wp-content/themes/yourchildtheme/woocommerce/checkout/ directory
。)
和
也需要在
checkout.min.js
(在wp-content/plugins/woocommerce/assets/js/frontend/
)中进行更改。在第 80 行(WooCommerce 4.5.2)评论了这个
/*if ( 0 === $payment_methods.filter( ':checked' ).length ) {
$payment_methods.eq(0).prop( 'checked', true );*/
不幸的是,不能通过将第二个文件复制到子主题文件夹中来覆盖它。此解决方案尚不支持更新。但也许可以继续使用并使其以智能和(尽可能)本地(尽可能)的方式进行更新。 这是我为此找到的一些解决方案.
p.s:即使客户更改邮政编码、地址数据等,此解决方案也会保留所选的付款方式