samedi 26 décembre 2015

Woocommerce to only run javascript if a specific payment method is selected

I have developed a payment gateway plugin for Woocommerce, and it relies heavily on some javascript which loads a popup to capture user details. I need that javascript to run only if THAT gateway is selected from the radio buttons. For some reason it doesn't work. Here's my code

jQuery(document).ready(function() {
  usingGateway();
  jQuery('form[name="checkout"] input[name="payment_method"]:checked').change(function(){
    usingGateway();
  });
});

function usingGateway(){
    console.log(jQuery("input[name='payment_method']:checked").val());
    if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'my_gateway'){
        console.log("Using my gateway");
        //Etc etc
    }else{
         console.log("Not using my gateway. Proceed as usual");
    }
}

It seems woocommerce overrides the javascript in some way, because even though the radio button is selected, my console.log for payment_method is always undefined. Then the IF block is skipped and it always says "Not using my gateway. When I comment out the IF statement, the payment gateway works fine.

But I need it to work because if a user selects another payment method (even BACS or offline payments), the javascript will still load the popup and attempt to process the payment using my custom gateway.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire