Woocommerce:为特定国家/地区设置可选的计费字段不起作用 [英] Woocommerce: Making a billing field optional for a specific country does not work

查看:21
本文介绍了Woocommerce:为特定国家/地区设置可选的计费字段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Wordpress Woocommerce 网站中,当用户选择特定的帐单国家/地区时,我需要跳过对帐单城市字段所需的验证.

In my Wordpress Woocommerce site I need to skip the required validation for billing city field when the user selects a specific billing country.

我在以下类似问题中提到了答案,但这两个问题似乎都不适用于我的结帐页面.我所做的只是将这些函数复制粘贴到活动主题的functions.php 中,并相应地更改字段和函数名称.但是更改或代码不会出现在结帐页面的源代码中,并且所需的验证仍然适用于不需要输入城市的国家/地区.

I have referred answers in the following similar questions but both these don't seem to work on my checkout page. All I did was copy pasting those functions in the functions.php of the active theme and changed the fields and function names accordingly. But the changes or the codes don't appear in the source code of the checkout page and the required validation still works for the countries where the city is not required to be entered.

Referred(以下答案均不适用于我的结帐页面)

Referred (Neither of the following answers work for my checkout page)

将结帐电话字段设为可选针对 WooCommerce 中的特定国家/地区

制作 Woocommerce 结帐电话字段根据送货国家/地区可选

我的代码

// Making the billing city field not required (by default)
add_filter( 'woocommerce_billing_fields', 'filter_billing_city_field');
function filter_billing_city_field( $fields ) {
    $fields['billing_city']['required'] = false;
    return $fields;
}

// Real time country selection actions
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 8, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
    $required = esc_attr__( 'required', 'woocommerce' );

    // HERE set the countries codes (2 capital letters) in this array:
    $countries = array( 'LK');

    // Hidden field for the phone number validation
    echo '<input type="hidden"  name="billing_city_check" id="billing_city_check" value="0">';
    $countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
    ?>
    <script type="text/javascript">
        (function($){
            var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
                countries = [<?php echo $countries_str; ?>],
                location = $('#billing_country option:selected').val(),
                cityCheck = 'input#billing_city_check';

            function actionRequire( actionToDo='yes', selector='' ){
                if ( actionToDo == 'yes' ) {
                    $(selector).addClass("validate-required");
                    $(selector+' label').append(required);
                } else {
                    $(selector).removeClass("validate-required");
                    $(selector+' label > .required').remove();
                }
                $(selector).removeClass("woocommerce-validated");
                $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
            }

            // Default value when loading
            actionRequire( 'no','#billing_city_check' );
            if( $.inArray( location, countries ) >= 0  && $(cityCheck).val() == '0' ){
                actionRequire( 'yes','#billing_city_check' );
                $(cityCheck).val('1');
            }

            // Live value
            $( 'form.checkout' ).on( 'change', '#billing_country', function(){
                var location = $('#billing_country option:selected').val();
                if ( $.inArray( location, countries ) >= 0 && $(cityCheck).val() == 0 ) {
                    actionRequire( 'yes','#billing_city_check' );
                    $(cityCheck).val('1');
                } else if ( $(cityCheck).val() == 1 ) {
                    actionRequire( 'no','#billing_city_check' );
                    $(cityCheck).val('0');
                }
            });
       })(jQuery);
        </script>
    <?php
}

// City validation, when it's visible
add_action('woocommerce_checkout_process', 'billing_city_field_process');
function billing_city_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['billing_city'] && $_POST['billing_city_check'] == '1' )
        wc_add_notice( __( 'Please enter city.' ), 'error' );
}

推荐答案

在您激活的主题 function.php 末尾添加此代码片段.

Add this code snippet in your activated theme function.php at the end.

add_filter('woocommerce_billing_fields', 'vg_customize_checkout_form');

function vg_customize_checkout_form($fields)
{
    $fields['billing_city']['required'] = false;
    return $fields;
}

#Source: https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-checkout.php#L845
add_action('woocommerce_after_checkout_validation', 'vg_custom_validation_billing_city', 10, 2);

function vg_custom_validation_billing_city($fields, $error)
{

    if('IN' != $fields['billing_country'] && empty($fields['billing_city'])) {
        $error->add('validation', 'City is required field.');
    }

}

<小时>

让我们看看每个钩子的功能:


Let's see each hook functionality:

过滤钩:woocommerce_billing_fields

官方文档:

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-8

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2

目的:我们可以通过覆盖数组元素来改变字段的预定义属性,因为我们已经覆盖了 billing_cityrequired 属性字段

Purpose: We can alter the predefined properties of the field by overriding the array element as we have overridden the required property of the billing_city field

Action Hook:woocommerce_after_checkout_validation

官方文档:

https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#830

目的:在购物车成功结帐后添加自定义代码.在这里我们可以添加我们的自定义验证,并根据验证成功或失败我们可以通过错误.

Purpose: To add custom code after the cart has been successfully checkout. Here we can add our custom validation and based on validation success or failure we can through error.

在这里,我们验证了帐单国家/地区是否不是印度,然后应填写城市.如果用户没有填写印度以外的城市,那么我们的自定义错误 City is a required field. 将被抛出.

Here we verified whether the billing country is not India then the city should be filled. If the user not filled the city for other than India then our custom error City is a required field. will be thrown.

参考:

  1. https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
  2. https://docs.woocommerce.com/wc-apidocs/hook-docs.html

这篇关于Woocommerce:为特定国家/地区设置可选的计费字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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