如果选择了特定的付款方式,Woocommerce 仅运行 javascript [英] Woocommerce to only run javascript if a specific payment method is selected

查看:53
本文介绍了如果选择了特定的付款方式,Woocommerce 仅运行 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Woocommerce 开发了一个支付网关插件,它严重依赖于一些加载弹出窗口来捕获用户详细信息的 javascript.仅当从单选按钮中选择了该网关时,我才需要运行该 javascript.由于某种原因,它不起作用.这是我的代码

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");
    }
}

woocommerce 似乎以某种方式覆盖了 javascript,因为即使选择了单选按钮,payment_method 的 console.log 始终未定义.然后跳过 IF 块,它总是说不使用我的网关.当我注释掉 IF 语句时,支付网关工作正常.

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.

但我需要它工作,因为如果用户选择另一种付款方式(即使是 BACS 或离线付款),javascript 仍会加载弹出窗口并尝试使用我的自定义网关处理付款.

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.

更新:根据 rnevius 评论,我检查了 woocommerce checkout.js 并将其从那里复制到我的插件 javascript 中:

Update: According to rnevius comment, I checked the woocommerce checkout.js and copied this from there into my plugin javascript:

var payment_methods = jQuery('.woocommerce-checkout').find( 'input[name="payment_method"]' );
if ( 0 === payment_methods.filter( ':checked' ).size() ) {
payment_methods.eq(0).attr( 'checked', 'checked' );
}

所以基本上如果没有检查任何东西,woocommerce 将强制检查第一项.不知道为什么我需要这样做,因为 woocommerce 已经在 checkout.js 中这样做了现在可以了,但是如果我的网关不是第一个选择的网关,更改"功能仍然不起作用(当您从另一个网关更改为我的网关时).请帮忙.

So essentially if nothing is already checked, woocommerce will force-check the first item. No idea why I need to do that since woocommerce already does that in checkout.js That works now but if my gateway isn't already the first one selected, the "change" function still isn't working (when you change to my gateway from another one). Please help.

推荐答案

试试这个来分配支付方式选择的回调

Try this to assign callback on payment method select

jQuery(function(){
    jQuery( 'body' )
    .on( 'updated_checkout', function() {
          usingGateway();

        jQuery('input[name="payment_method"]').change(function(){
            console.log("payment method changed");
              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");
    }
}   

这篇关于如果选择了特定的付款方式,Woocommerce 仅运行 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆