猿问

如何在 WooCommerce 结账时根据国家/地区创建必填自定义字段?

我已将自定义字段添加到结帐页面 (billing_vat),当国家/地区设置为爱尔兰 (IE) 时需要填写该字段。


目前,我已经更改了标签以表明它与使用 JavaScript 的所有其他字段一样是必需的,并且已连接到“woocommerce_get_country_locale”以将该字段更改为 IE 所需。


add_filter('woocommerce_billing_fields', 'dc_custom_billing_fields', 1000, 1);

function dc_custom_billing_fields( $fields ) {

    $fields['billing_vat'] = array(

        'label'        => 'VAT Number',

        'required'     => false,

        'type'         => 'text',

        'class'        => array( 'form-row-wide' ),

        'priority'     => 35,

    );


    return $fields;

}


add_filter( 'woocommerce_get_country_locale', 'dc_change_locale_field_defaults', 1000, 1 );

function dc_change_locale_field_defaults($countries) {

    $countries['IE']['billing_vat']['required'] = true;


    return $countries;

}


add_action( 'woocommerce_admin_order_data_after_shipping_address', 'dc_display_admin_order_meta', 10, 1 );

function dc_display_admin_order_meta($order) {

    echo '<p><strong>'.__('Billing VAT').':</strong> ' . get_post_meta( $order->get_id(), '_billing_vat', true ) . '</p>';

}


add_action( 'woocommerce_after_order_notes', 'dc_after_checkout_field' );

function dc_after_checkout_field() {

    ?>

    <script>

        (function($) {

            $(document).ready(function (){

                $('#billing_country').on('change',function() {

                    if ($('#billing_country').val() == 'IE') {

                        // Required

                        $('#billing_vat').prop('required', true);

                        $('label[for="billing_vat"] .optional').remove();

                        $('label[for="billing_vat"]').append('<abbr class="required" title="required">*</abbr>');

                    } 

                    }

                })

            });

        })(jQuery);

    </script>

    <?php

}

但是,当我提交表格时,国家/地区设置为爱尔兰且该字段为空,Woo 并没有说该字段是必需的。


慕虎7371278
浏览 157回答 2
2回答

阿波罗的战车

要使自定义字段billing_vat成为必填项,当国家/地区设置为爱尔兰 (IE) 时。只需将您现有的代码替换为:(通过注释标签进行解释,添加到代码中)// Add fieldfunction filter_woocommerce_billing_fields( $fields ) {&nbsp; &nbsp; $fields['billing_vat'] = array(&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp; => 'VAT Number',&nbsp; &nbsp; &nbsp; &nbsp; 'required'&nbsp; &nbsp; &nbsp;=> false,&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'text',&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp; &nbsp; => array( 'form-row-wide' ),&nbsp; &nbsp; &nbsp; &nbsp; 'priority'&nbsp; &nbsp; &nbsp;=> 35,&nbsp; &nbsp; );&nbsp; &nbsp; return $fields;}add_filter( 'woocommerce_billing_fields', 'filter_woocommerce_billing_fields', 10, 1 );// Validatefunction action_woocommerce_after_checkout_validation( $data, $error ) {&nbsp; &nbsp; if ( $data['billing_country'] == 'IE' && empty( $data['billing_vat'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $error->add( 'validation', 'Required based on country.' );&nbsp; &nbsp; }}add_action('woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );// jQueryfunction action_woocommerce_after_order_notes( $checkout ) {&nbsp; &nbsp; ?>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; (function($) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required_or_optional(); //this calls it on load&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '#billing_country' ).change( required_or_optional );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function required_or_optional() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $( '#billing_country' ).val() == 'IE' ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Required&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '#billing_vat' ).prop( 'required', true );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'label[for="billing_vat"] .optional' ).remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'label[for="billing_vat"]' ).append( '<abbr class="required" title="required">*</abbr>' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '#billing_vat' ).removeProp( 'required' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'label[for="billing_vat"] .required' ).remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Avoid append this multiple times&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $( 'label[for="billing_vat"] .optional' ).length == 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'label[for="billing_vat"]' ).append( '<span class="optional">(optional)</span>' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; })(jQuery);&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php}add_action( 'woocommerce_after_order_notes', 'action_woocommerce_after_order_notes', 10, 1 );// Display on the order edit page (backend)function action_woocommerce_admin_order_data_after_shipping_address( $order ) {&nbsp; &nbsp; if ( $value = $order->get_meta( '_billing_vat' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><strong>' . __( 'Billing VAT', 'woocommerce' ) . ':</strong> ' . $value . '</p>';&nbsp; &nbsp; }}add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

子衿沉夜

只需将以下代码片段添加到您现有的代码片段中,希望您将完成管理爱尔兰的验证add_action('woocommerce_checkout_process', 'billing_vat_field_process');function billing_vat_field_process() {&nbsp; &nbsp; // Check if set, if its not set add an error.&nbsp; &nbsp; if ( ! $_POST['billing_vat']&nbsp; && !empty($_POST['billing_country']) &&&nbsp;&nbsp; &nbsp; $_POST['billing_country'] == "IE" ){&nbsp; &nbsp; &nbsp; wc_add_notice( __( 'Please enter VAT number' ), 'error' );&nbsp; &nbsp;}}
随时随地看视频慕课网APP
我要回答