自定义结帐字段在 Woocommerce 3 中启用或禁用付款方式 [英] Custom checkout field enable or disable payment methods in Woocommerce 3

查看:24
本文介绍了自定义结帐字段在 Woocommerce 3 中启用或禁用付款方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究 http://www.lichtunie.nl我们有一个功能正常的结帐页面,其中包含所需的字段.问题是这样的:在荷兰(我们所在的地方),我们有一个叫做 KvK 的东西,如果你创办一家公司,你需要在那里注册它,然后你会得到一个 KvK 号码.我们可以通过网站检查这些数字,看看它们是否合法以及它们的付款历史如何.

So i'm working on http://www.lichtunie.nl We have a functioning checkout page with the needed fields. 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. We can check those numbers through a website to see if they're legit and how their payment history is.

现在我们可以选择用支票付款",您可以在收到发票后的 30 天内订购并付款.我们现在想要的是,当有人在结账时没有填写他们的 KvK 号码字段时,他们就不能使用这种付款方式.

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. 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.

我一直在寻找一段时间,但不知道如何做到这一点.有人有任何提示吗?

I've been looking for a while and just can't figure out how to do this. Anyone got any tips?

提前致谢,

莱克斯

推荐答案

如果填写或存在账单 KVK 号码结帐字段,以下代码将仅保留支票"付款方式:

The following code will keep only "cheque" payment method if the billing KVK number checkout field is filled or exist:

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

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

添加
如果未填写帐单 KVK 号码结帐字段,还要隐藏支票"付款方式,请将第一个功能替换为:

Addition
Also to hide "cheque" payment method if billing KVK number checkout field is not filled, replace the first function with this one:

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

它应该可以(未经测试).

这篇关于自定义结帐字段在 Woocommerce 3 中启用或禁用付款方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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