首页 文章

如何防止Woocommerce在Checkout页面上选择默认付款方式?

提问于
浏览
2

在结帐页面上显示付款方式,默认情况下会自动选择第一个付款方式 . 我需要阻止选择,因此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模板文件中删除代码:

选中($ gateway-> selected,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);

2 回答

  • 4

    好的,搞定了 . 方法如下:

    • 从以下位置复制javascript文件:

    /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页面时不应检查默认付款方式 .

  • 2

    我有同样的问题,我已经解决了这个问题:

    • 用我的子主题checkout-override-js覆盖checkout-js文件 .

    • 然后注释掉以下代码:

    行:48: this.init_payment_methods();
    行:58:

    init_payment_methods: function() {
                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();

    这将删除默认的付款方式选择 . 您可以使用这些代码使其符合您的要求 .

相关问题