【问题标题】:Woocommerce: Making a billing field optional for a specific country does not workWoocommerce:使特定国家/地区的计费字段可选不起作用
【发布时间】:2020-08-28 04:44:27
【问题描述】:

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

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

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

Make checkout phone field optional for specific countries in WooCommerce

Make Woocommerce checkout phone field optional based on shipping country

我的代码

// 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' );
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    在你激活的主题function.php最后添加这个代码sn-p。

    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.');
        }
    
    }
    

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

    过滤器钩子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_city 字段的required 属性


    动作挂钩woocommerce_after_checkout_validation

    官方文档

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

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

    这里我们验证了计费国家是否不是印度,那么应该填写城市。如果用户没有填写印度以外的城市,则会抛出我们的自定义错误City is a required field.


    参考

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-05
      • 2022-11-14
      • 2020-12-27
      • 2017-05-31
      • 2021-10-27
      • 2015-04-09
      • 2019-07-29
      • 2019-05-26
      相关资源
      最近更新 更多