Payment method is not required for selection Woocommerce 3

Example

The payment method is not required for selection. That is, change "select" to "checkbox" on checkout page. And the order can be made even if the payment method is not selected.

I suspect that this is not enough, since it is possible, it will cause an error or a problem if you do not choose a payment method.

Therefore, I want to add the ability to create an order without choosing payment methods. And, accordingly, the system should understand that for the order it is necessary to assign a status, for example "On Hold".

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>

I have one idea:

Create a custom payment method - "default".Only two payment methods will be visible on the website as in the picture (without the third payment method).Then redefine the plugin template and add a check, if the checkbox is not selected, then there remains "default".

Upvotes: 1

Views: 1170

Answers (2)

Karan Trivedi
Karan Trivedi

Reputation: 11

add_filter( ‘woocommerce_cart_needs_payment’, ‘__return_false’ );

Please Add Above filter in functions.php file of the activated theme.

Upvotes: 1

LoicTheAztec
LoicTheAztec

Reputation: 254493

Updated: First you should need a custom payment gateway:

You can download the plugin from HERE, install it and activate it.

Once done you will add the following code. It will hide this "Custom" payment method and make it selected by default on checkout page.

Some jQuery code will allow to switch between your existing payment gateways and this hidden "Custom" one.

The code:

// 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
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

Upvotes: 3

Related Questions