简体   繁体   English

自定义结帐字段在 Woocommerce 3 中启用或禁用付款方式

[英]Custom checkout field enable or disable payment methods in Woocommerce 3

So i'm working on http://www.lichtunie.nl We have a functioning checkout page with the needed fields.所以我正在http://www.lichtunie.nl 上工作,我们有一个功能正常的结帐页面,其中包含所需的字段。 The problem is this: In the Netherlands (where we're based) We have something called KvK, if you start a company you need to register it there and you get a KvK number.问题是这样的:在荷兰(我们的总部),我们有一个叫做 KvK 的东西,如果你创办一家公司,你需要在那里注册它,你会得到一个 KvK 号码。 We can check those numbers through a website to see if they're legit and how their payment history is.我们可以通过网站检查这些数字,看看它们是否合法以及它们的付款历史如何。

Now we have the option of "paying with cheque" Which let's you order and pay within a 30 day time period after receiving the invoice.现在我们可以选择“用支票付款”,让您在收到发票后的 30 天内订购并付款。 What we want now is that when someone doesn't fill in their KvK number field on checkout they can't use this method of payment.我们现在想要的是,当有人在结账时没有填写他们的 KvK 号码字段时,他们就不能使用这种付款方式。

As soon as they've filled in the "KvK number" field they should be able to though.只要他们填写了“KvK 号码”字段,他们就应该可以。

I've been looking for a while and just can't figure out how to do this.我一直在寻找一段时间,只是无法弄清楚如何做到这一点。 Anyone got any tips?有人有任何提示吗?

Thanks in advance,提前致谢,

Lex莱克斯

The following code will keep only "cheque" payment method if the billing KVK number checkout field is filled or exist:如果帐单 KVK 号码结帐字段已填写或存在,以下代码将仅保留“支票”付款方式:

add_filter( 'woocommerce_available_payment_gateways', 'kvk_field_cheque_payment_method', 20, 1);
function kvk_field_cheque_payment_method( $gateways ){
    foreach( $gateways as $gateway_id => $gateway ) {
    // Not in backend (admin)
    if( is_admin() ) 
        return $gateways;

        if( WC()->session->get( 'is_kvk_nummer' ) && $gateway_id != 'cheque' ){
            unset( $gateways[$gateway_id] );
        }
    }
    return $gateways;
}

// The Wordpress Ajax PHP receiver
add_action( 'wp_ajax_kvk_nummer', 'get_ajax_kvk_nummer' );
add_action( 'wp_ajax_nopriv_kvk_nummer', 'get_ajax_kvk_nummer' );
function get_ajax_kvk_nummer() {
    if ( $_POST['bkvkn'] == '1' ){
        WC()->session->set('is_kvk_nummer', '1');
    } else {
        WC()->session->set('is_kvk_nummer', '0');
    }
    die();
}

// The jQuery Ajax request
add_action( 'wp_footer', 'checkout_kvk_fields_script' );
function checkout_kvk_fields_script() {
    // Only checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ):

    // Remove "is_kvk_nummer" custom WC session on load
    if( WC()->session->get('is_kvk_nummer') ){
        WC()->session->__unset('is_kvk_nummer');
    }
    ?>
    <script type="text/javascript">
        jQuery( function($){
            var a = 'input#billing_kvk_nummer';

            // Ajax function
            function checkKvkNummer( value ){
                 $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'kvk_nummer',
                        'bkvkn': value != '' ? 1 : 0,
                    },
                    success: function (result) {
                        $('body').trigger('update_checkout');
                    }
                });
            }

            // On start
            checkKvkNummer($(a).val());

            // On change event
            $(a).change( function () {
                checkKvkNummer($(this).val());
            });
        });
    </script>
    <?php
    endif;
};

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

Addition添加
Also to hide "cheque" payment method if billing KVK number checkout field is not filled, replace the first function with this one:如果未填写帐单 KVK 号码结帐字段,还要隐藏“支票”付款方式,请将第一个功能替换为:

add_filter( 'woocommerce_available_payment_gateways', 'kvk_field_cheque_payment_method', 20, 1);
function kvk_field_cheque_payment_method( $gateways ){
    // Not in backend (admin)
    if( is_admin() ) 
        return $gateways;

    foreach( $gateways as $gateway_id => $gateway ) {

        if( $gateway_id != 'cheque' && WC()->session->get( 'is_kvk_nummer' ) ){
            unset( $gateways[$gateway_id] );
        } elseif( $gateway_id == 'cheque' && ! WC()->session->get( 'is_kvk_nummer' ) ){
            unset( $gateways[$gateway_id] );
        }
    }
    return $gateways;
}

It should work (untested) .它应该可以工作(未经测试)

Try this code your active theme custom js file试试这个代码你的活动主题自定义 js 文件

    $( "#KvK_number" ).change(function() { //Here assign the KvK number ID 
        if (this.val() == "") {
            $('#paying_with_cheque').hide(); /// Here give the Check payment div id
        }
        else
        {
        $('#paying_with_cheque').show();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM