亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據 WooCommerce 結賬中選擇的城市顯示子區域下拉列表

根據 WooCommerce 結賬中選擇的城市顯示子區域下拉列表

PHP
Cats萌萌 2023-12-15 16:27:24
我正在創建一個 WooCommerce 插件,我想根據結賬頁面中選擇的客戶城市動態顯示子區域。這是我的代碼嘗試:add_filter( 'woocommerce_checkout_fields', 'dvs_city_list' );function dvs_city_list( $fields ) {     $fields["billing"]["billing_city"]["type"] = 'select';    $fields["billing"]["billing_city"]["input_class"] = array(    'state_select' => 'state_select'    );      $fields["billing"]["billing_city"]["options"] = array(        'Lahore' => 'Lahore',        'Karachi' => 'Karachi'    ),    return $fields;}add_filter( 'woocommerce_checkout_fields', 'dvs_area_list' );function dvs_area_list( $fields ) {     $fields['billing']['billing_area']['label'] = 'Area';    $fields['billing']['billing_area']['required'] = 'True';    $fields["billing"]["billing_area"]["type"] = 'select';    $fields["billing"]["billing_area"]["class"][0] = 'form-row-last';    $fields['billing']['billing_area']['priority'] = 50;    $fields["billing"]["billing_area"]["input_class"] = array(    'state_select' => 'state_select'    );        $city = $_REQUEST['billing_city'];        if ($city == 'Lahore') {        $fields["billing"]["billing_area"]["options"] = array(        'Naval Town' => 'Naval Town',        'Bahria Town' => 'Bahria Town',        'Faisal Town' => 'Faisal Town'        );    }    else ($city == 'Karachi') {    $fields["billing"]["billing_area"]["options"] = array(        'Walton Road' => 'Walton Road',        'Zest Road' => 'Zest Road'        );     }     return $fields;}這是截圖但我收到這個錯誤注意:未定義索引:…wp-content/plugins/custom-plugin/index.php 第 35 行中的 billing_city如何修復這個錯誤?我做錯了什么?
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

要從另一個選擇字段同步自定義結帳選擇字段,需要使用 jQuery。


您還可以合并這兩個函數,因為它們使用相同的鉤子。


在下面的第一個函數中,我們保留您可以在任何地方調用的城市/地區設置。最后一個功能啟用“計費區域”上的動態選項更改。下拉列表取決于所選城市:


function cities_areas_settings() {

    $text_domain = 'woocommerce';


    return array(

        __('Lahore', $text_domain) => array(

            __('Naval Town', $text_domain),

            __('Bahria Town', $text_domain),

            __('Faisal Town', $text_domain),

        ),

        __('Karachi', $text_domain) => array(

            __('Walton Road', $text_domain),

            __('Zest Road', $text_domain),

        )

    );

}


add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' );

function custom_checkout_fields( $fields ) {

    // Initializing

    $text_domain   = 'woocommerce';

    $option_cities = array();

    $lahore_areas  = array( '' => __('Choose your area', $text_domain) );


    // Load settings and prepare options arrays

    foreach( cities_areas_settings() as $city => $areas ) {

        $option_cities[$city] = $city;

        if( $city === 'Lahore' ) {

            foreach( $areas as $area ) {

                $lahore_areas[$area] = $area;

            }

        }

    }


    // 1. Billing City field

    $fields['billing']['billing_city']['type']        = 'select';

    $fields['billing']['billing_city']['class']       = array('form-row-first');

    $fields['billing']['billing_city']['input_class'] = array('state_select');

    $fields['billing']['billing_city']['options']     = $option_cities;


    // 2. Billing Area Field

    $fields['billing']['billing_area'] = array(

        'type'        => 'select',

        'label'       => __('Area', $text_domain),

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

        'input_class' => array('state_select'),

        'options'     => $lahore_areas,

        'required'    => true,

        'default'     => '',

        'priority'    => 50,

    );


    return $fields;

}


add_action('wp_footer', 'custom_checkout_js_script');

function custom_checkout_js_script() {

    if( is_checkout() && ! is_wc_endpoint_url() ) :

    // Initializing

    $text_domain   = 'woocommerce';

    $karachi_areas = array( '' => __('Choose your area', $text_domain) );


    $settings = cities_areas_settings(); // Load settings


    // Prepare 'Karachi' options dropdown

    foreach( cities_areas_settings()['Karachi'] as $area ) {

        $karachi_areas[$area] = $area;

    }


    ?>

    <script language="javascript">

    jQuery( function($){

        var a = 'select[name="billing_city"]',

            b = 'select[name="billing_area"]',

            o = <?php echo json_encode($karachi_areas); ?>,

            s = $(b).html();


        // Utility function to fill dynamically the select field options

        function dynamicSelectOptions( opt ){

            var options = '';

            $.each( opt, function( key, value ){

                options += '<option value="'+key+'">'+value+'</option>';

            });

            $(b).html(options);

        }


        // On Start (once DOM is loaded)

        if ( $(a).val() === 'Karachi' ) {

            dynamicSelectOptions( o );

        }


        console.log($(a).val());


        // On billing city change live event

        $('form.woocommerce-checkout').on('change', a, function() {

            console.log($(this).val());

            if ( $(this).val() === 'Karachi' ) {

                dynamicSelectOptions( o );

            } else {

                $(b).html(s);

            }

        });

    });

    </script>

    <?php

    endif;

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-12-15
  • 1 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號