【问题标题】:Payment method is not required for selection Woocommerce 3选择 Woocommerce 3 不需要付款方式
【发布时间】:2018-12-26 13:46:13
【问题描述】:

选择付款方式不是必需的。即在结帐页面上将“选择”更改为“复选框”。即使没有选择付款方式也可以下单。

我怀疑这还不够,既然有可能,如果不选择付款方式,就会导致错误或问题。

因此,我想添加无需选择付款方式即可创建订单的功能。并且,相应地,系统应该理解,对于订单,有必要分配一个状态,例如“等待”。

wp-content\plugins\woocommerce\templates\checkout\payment-method.php

<li class="wc_payment_method payment_method_<?php echo $gateway->id; ?>">
    <input id="payment_method_<?php echo $gateway->id; ?>" type="radio" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gateway->id ); ?>" <?php checked( $gateway->chosen, true ); ?> data-order_button_text="<?php echo esc_attr( $gateway->order_button_text ); ?>" />

    <label for="payment_method_<?php echo $gateway->id; ?>">
        <?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?>
    </label>
    <?php if ( $gateway->has_fields() || $gateway->get_description() ) : ?>
        <div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
            <?php $gateway->payment_fields(); ?>
        </div>
    <?php endif; ?>
</li>

我有一个想法:

创建自定义支付方式-“默认”。如图所示,网站上只会显示两种支付方式(没有第三种支付方式)。然后重新定义插件模板并添加一个检查,如果复选框不是选中,则保持“默认”。

【问题讨论】:

    标签: php wordpress checkbox woocommerce checkout


    【解决方案1】:

    更新:首先您需要一个自定义支付网关:

    您可以下载插件from HERE,安装并激活。

    完成后,您将添加以下代码。它将隐藏此“自定义”付款方式,并在结帐页面默认选中。

    一些 jQuery 代码将允许在您现有的支付网关和这个隐藏的“自定义”网关之间切换。

    代码:

    // set "custom" payment method as default on checkou and hide it
    add_action( 'woocommerce_before_checkout_form', 'set_custom_payment_method_as_default_and_hide_it' );
    function set_custom_payment_method_as_default_and_hide_it(){
        // Set "custom" payment method as default
        WC()->session->set('chosen_payment_method', 'custom');
    
        // Hide this "Custom" payment method
        ?>
        <style>
            ul.wc_payment_methods > li.payment_method_custom { display: none !important; }
        </style>
        <?php
    }
    
    // switch between your existing payment gateways and the "custom" one
    add_action( 'wp_footer', 'auto_switch_payment_methods' );
    function auto_switch_payment_methods(){
        // Only on checkout page
        if ( ! is_checkout() ) return;
        // jQuery code
        ?>
        <script type="text/javascript">
            jQuery(function($){
                var a = 'form.checkout', b = 'input[name="payment_method"]', c = 'input#payment_method_custom';
                $(a).on( 'click', b, function() {
                    if( ! $(c).is(':checked') && $(this).hasClass('on') ) {
                            $(c).prop("checked", true);
                            $(this).removeClass('on');
                            $('div.payment_box').each( function(){
                                $(this).hide('fast');
                            });
                    } else if( ! $(c).is(':checked') && ! $(this).hasClass('on') ) {
                        $(b).each( function(){
                            $(this).removeClass('on');
                        });
                        $(this).addClass('on')
                    }
                });
            });
        </script>
        <?php
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

    【讨论】:

      【解决方案2】:

      add_filter('woocommerce_cart_needs_payment', '__return_false');

      请在已激活主题的functions.php 文件中添加上述过滤器。

      【讨论】:

        猜你喜欢
        • 2021-01-27
        • 2018-08-30
        • 2016-10-15
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 2021-03-08
        相关资源
        最近更新 更多