Using the WooCommerce API to get the country/state lists

前端 未结 2 1642
我寻月下人不归
我寻月下人不归 2020-12-29 14:29

I\"m using the WooCommerce API to get the country list. I am doing:

$woo_countries = new WC_Countries();
$countries = $woo_countries->get_allowed_countrie         


        
2条回答
  •  死守一世寂寞
    2020-12-29 15:09

    This is referred to as the "base country" by WooCommerce:

    $woo_countries = new WC_Countries();
    
    // Get default country
    $default_country = $woo_countries->get_base_country();
    
    // Get states in default country
    $states = $woo_countries->get_states( $default_country );
    

    Read more in the API docs.

    It should be noted that the default WooCommerce billing form template logic ensures that the default country and state are pre-selected. Here's what it looks like:

    checkout_fields['billing'] as $key => $field ) : ?>
    
        get_value( $key ) ); ?>
    
    
    

    The third argument passed to woocommerce_form_field() ensures that the default country and state are pre-populated.

提交回复
热议问题