【问题标题】:Why required country dropdown Field not preventing checkout - WooCommerce为什么需要国家/地区下拉字段不阻止结帐-WooCommerce
【发布时间】:2021-03-24 11:22:37
【问题描述】:

标题可能看起来很简单,但它表明它是一个必填字段 - 但是允许结帐而不选择选项并且仅显示选择选项。

我正在使用Change WooCommerce checkout country field default option displayed label 回答我上一个问题的代码,这样:

add_filter( 'woocommerce_form_field_country', 'filter_form_field_country', 10, 4 );
function filter_form_field_country( $field, $key, $args, $value ) {
    // Only in checkout page for "billing" city field
    if ( is_checkout() && 'billing_country' === $key ) {
        $search  = esc_html__( 'Select a country / region…', 'woocommerce' ); // String to search
        $replace = esc_html__( 'Select option to pay correct tax', 'woocommerce' ); // Replacement string
        $field   = str_replace( $search, $replace, $field );
    }
    return $field;
}

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_country']['label'] = __('Are you purchasing as a Company Y/N or from the UK?', 'woocommerce');
    $fields['billing']['billing_country']['required'] = true;

    return $fields;
}

有人发现有冲突吗?

【问题讨论】:

    标签: php wordpress woocommerce checkout required-field


    【解决方案1】:

    在Woocommerce中,国家下拉目字段默认选项没有空值,但是“默认”为值,因为您可以看到in this source code ...所以当所选值为“默认”时,您需要添加以下内容,以避免结帐显示错误通知:

    add_action( 'woocommerce_after_checkout_validation', 'checkout_validation_billing_country', 9999, 2 );
    function checkout_validation_billing_country( $data, $errors ){
        // Check for any validation errors
        if ( isset($_POST['billing_country']) && $_POST['billing_country'] === 'default' ){
            $errors->add( 'validation', __('<strong>Billing Country</strong> is a required field. Please select your country.', 'woocommerce') );
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 再次 LoicTheAztec 你的明星 - 工作了一次 - 很抱歉延迟回复,但已经离开工作室。认为我已经投票并接受了答案。
    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2015-04-09
    • 2021-10-27
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多