In a recent WooCommerce development project, I wanted to get all the countries and display the states and this required a creative way to create a quick solution. I created the solution I will share with you in this quick guide and also the WooCommerccode snippet that you can reuse in your WooCommerce development projects or customization of WooCommerce to help you save time and be more productive.
First, it is necessary we understand the methods we can use to get the countries in WooCommerce before we can check out the WooCommerce states list code. The following is the method that you use to get WooCommerce states list :
[php] // Get the states for a country.WC_Countries::get_states($cc) [/php]
You can also use the following code to get the countries by creating an object from the global WooCommerce object as in the code snippet below.
You can also pass the code of the country to the get_states() function and display the states for the specific country as shown in the image below where I have placed the US as the country in the get_states function:
[php] /*** Action Hook to Display the countries list in WooCommerce in the header
*/
add_action(‘wp_head’, ‘njengah_header_example_code’);
function njengah_header_example_code(){
global $woocommerce;
$countries_obj = new WC_Countries();
$wc_states_list = $countries_obj->get_states( ‘US’ );
print(‘<pre>’);
print_r($wc_states_list);
print(‘</pre>’);
}
[/php]When you add this code in the action hook and display it in the header to test it you can see that the countries are displayed as shown in the image below :
Conclusion
In this post, we have looked at how you can get the states from the country object in WooCommerce and display them in the different locations in your WooCommerce theme or your WooCommerce store.
If you need further assistance with this issue, I can quickly check and advise what is the best way to get this done in your specific WooCommerce theme. I would be glad to help you, feel free to reach out for further assistance, you can get in touch for a free consultation.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Delete a Page In WordPress
- How to Customize WooCommerce Order Received Page
- How to Get Product Brand Name in WooCommerce
- How to Customize Product Category Page In WooCommerce
- How to Show Product Stock on the Cart Page WooCommerce
- How to Display All Products With No Weight WooCommerce
- How to Add WooCommerce Shopping Cart Icon In Menu
- How to Get Product Description & Display WooCommerce
- How to Disable Click on Product Image WooCommerce
- How to Redirect a WordPress Page Without Plugins?
- WordPress Get Post Category Snippet & Guide
- How to Check If Product is Variable In WooCommerce
- How to Display Username In WooCommerce
- How to Change SKU Text WooCommerce
- How to Add Star Rating To WooCommerce Product
- How to Add WooCommerce Registration Email Verification