第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 WooCommerce 結(jié)賬時(shí)根據(jù)國(guó)家/地區(qū)創(chuàng)建必填自定義字段?

如何在 WooCommerce 結(jié)賬時(shí)根據(jù)國(guó)家/地區(qū)創(chuàng)建必填自定義字段?

PHP
慕虎7371278 2023-04-21 13:56:56
我已將自定義字段添加到結(jié)帳頁(yè)面 (billing_vat),當(dāng)國(guó)家/地區(qū)設(shè)置為愛(ài)爾蘭 (IE) 時(shí)需要填寫(xiě)該字段。目前,我已經(jīng)更改了標(biāo)簽以表明它與使用 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}但是,當(dāng)我提交表格時(shí),國(guó)家/地區(qū)設(shè)置為愛(ài)爾蘭且該字段為空,Woo 并沒(méi)有說(shuō)該字段是必需的。
查看完整描述

2 回答

?
阿波羅的戰(zhàn)車(chē)

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

要使自定義字段billing_vat成為必填項(xiàng),當(dāng)國(guó)家/地區(qū)設(shè)置為愛(ài)爾蘭 (IE) 時(shí)。


只需將您現(xiàn)有的代碼替換為:(通過(guò)注釋標(biāo)簽進(jìn)行解釋?zhuān)砑拥酱a中)


// Add field

function filter_woocommerce_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_billing_fields', 'filter_woocommerce_billing_fields', 10, 1 );


// Validate

function action_woocommerce_after_checkout_validation( $data, $error ) {

    if ( $data['billing_country'] == 'IE' && empty( $data['billing_vat'] ) ) {

        $error->add( 'validation', 'Required based on country.' );

    }

}

add_action('woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );


// jQuery

function action_woocommerce_after_order_notes( $checkout ) {

    ?>

    <script>

        (function($) {

            $(document).ready(function () {

                required_or_optional(); //this calls it on load

                $( '#billing_country' ).change( required_or_optional );


                function required_or_optional() {

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

                    } else {

                        $( '#billing_vat' ).removeProp( 'required' );

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


                        // Avoid append this multiple times

                        if ( $( 'label[for="billing_vat"] .optional' ).length == 0 ) {

                            $( 'label[for="billing_vat"]' ).append( '<span class="optional">(optional)</span>' );

                        }

                    }

                }

            });

        })(jQuery);

    </script>

    <?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 ) {

    if ( $value = $order->get_meta( '_billing_vat' ) ) {

        echo '<p><strong>' . __( 'Billing VAT', 'woocommerce' ) . ':</strong> ' . $value . '</p>';

    }

}

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



查看完整回答
反對(duì) 回復(fù) 2023-04-21
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

只需將以下代碼片段添加到您現(xiàn)有的代碼片段中,希望您將完成管理愛(ài)爾蘭的驗(yàn)證


add_action('woocommerce_checkout_process', 'billing_vat_field_process');

function billing_vat_field_process() {

    // Check if set, if its not set add an error.

    if ( ! $_POST['billing_vat']  && !empty($_POST['billing_country']) && 

    $_POST['billing_country'] == "IE" ){

      wc_add_notice( __( 'Please enter VAT number' ), 'error' );

   }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-21
  • 2 回答
  • 0 關(guān)注
  • 189 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)