-2

I working on a Woocommerce project and my client asked me two things:

  1. Does not want to uncheck all the payment methods available on the checkout page.
  2. Wants to show a confirmation pop up( <script>confirm("Are you sure?")</script> ) on selecting 'cod' as payment method, after clicking 'no' or 'cancel' on the confirmation pop change the payment method as online payment or others accept 'cod'.

Please any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sushil
  • 39
  • 9

2 Answers2

0

I have managed to achieve what you need. I don't know whether it is the right way.

add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );

function refresh_payment_methods(){
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function(event) {
                var payment_method = $('form.checkout').find('input[name^="payment_method"]:checked').val();
                if(payment_method == 'cod'){
                    var method_select = confirm('Are you sure ?');
                    if(!method_select){
                        $('form.checkout').find('input[name^="payment_method"]').each(function(index, el) {
                            if($(this).val() == 'cod'){
                                $(this).prop('checked',false);
                            }else{
                                $(this).prop('checked',true);
                            }
                        });

                    }else{
                        $('body').trigger('update_checkout');   
                    }
                }else{
                    $('body').trigger('update_checkout');
                }
            });
        })(jQuery);
    </script>
    <?php
}

On changing the form input with name payment_method, the checked value is taken and comparing it with available payment methods. If the payment method is cod then corresponding checkbox is unchecked. (Since the code was too urgent for you, i couldn't make the else part perfect)

melvin
  • 2,571
  • 1
  • 14
  • 37
  • @SushilGupta I highly recommend you to optimise the code as possible as you can. Also please do a deep testing since it affect payment methods. Regards – melvin Aug 23 '18 at 10:34
  • dear @melvin the code is working fine but it causes problem when working with coupon, I want to remove all the coupons from the cart if COD is selected and reapply if 'Online Payment' mode is selected – Sushil Aug 23 '18 at 12:38
  • @SushilGupta that is a bit work to do. Is it an urgent one or can u give me time ? – melvin Aug 24 '18 at 04:28
  • @SushilGupta Happy to help. Feel free to ask – melvin Aug 24 '18 at 06:51
0

The above code did not work correctly for me!

In the following code, if the cancel button is selected, the radio button will return to the previous state.

The following code was executed correctly:

add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );

function refresh_payment_methods(){
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    ?>
    <script type="text/javascript">
     var j_method = <?='"'. $chosen_payment_method .'"'?>;
        (function($){
        $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function(event) {
            var payment_method = $('form.checkout').find('input[name^="payment_method"]:checked').val();
            if(payment_method == 'cod')
            {
              var method_select = confirm( 'Are you sure ?' );
                
                if( method_select )//OK:
                 setTimeout(function(){$('body').trigger('update_checkout'); }, 250 );     
                else//Cancel:
                 $( '#payment_method_' + j_method ).prop('checked',true);

            }else{
              setTimeout(function(){$('body').trigger('update_checkout'); }, 250 );
            }
            //set new value
            j_method = $('form.checkout').find('input[name^="payment_method"]:checked').val();
        });
    })(jQuery);
</script>
<?php
}
mmd
  • 47
  • 8