Blog

How to get WooCommerce categories?

WooCommerce is one of the most powerful and widely-used eCommerce platforms built on top of WordPress. Whether you’re a developer building a custom theme or a business owner customizing your online store, accessing WooCommerce categories programmatically or through the admin interface is often necessary. Understanding how to retrieve these categories ensures better control over your products and helps enhance customer experience by improving navigation and organization.

What Are WooCommerce Categories?

WooCommerce categories are taxonomies, just like WordPress post categories. Each product can belong to one or more categories for better organization. Categories enable store owners to group similar items, making it easier for customers to browse and find products.

Getting WooCommerce Categories via the WordPress Admin Panel

If you are a store manager or site administrator, the easiest way to view or manage product categories is through the WordPress dashboard. Here’s how you can access them:

  1. Log in to your WordPress admin panel.
  2. Navigate to Products > Categories.
  3. You will see a list of all existing categories, along with options to add, edit, or delete them.

This method is perfect for basic category management without needing any coding knowledge.

Retrieving WooCommerce Categories Programmatically

For developers and technical users, WooCommerce provides robust functions that allow you to access product categories directly from your theme or plugin code.

Using get_terms() Function

You can retrieve product categories using the get_terms() function in WordPress, applied to the product_cat taxonomy. Here’s a basic example:

<?php
$categories = get_terms( array(
    'taxonomy'   => 'product_cat',
    'hide_empty' => false,
) );
foreach ( $categories as $category ) {
    echo '<p>' . esc_html( $category->name ) . '</p>';
}
?>

Note: Setting hide_empty to false ensures that categories with no products are also retrieved. Use true to retrieve only categories containing products.

Filtering Parent and Child Categories

If you’re looking to only retrieve parent or child categories, you can modify the arguments like so:

  • Parent Categories:
    'parent' => 0
        
  • Child Categories of a Specific Parent:
    'parent' => <parent_category_id>
        

This functionality is useful when you need a hierarchical display of categories on the front end of your store.

Displaying Categories in a Custom Menu or Page

After retrieving the categories, you might want to display them in a structured format, such as a menu or layout on your homepage. Here’s a basic example of how to loop through and display them with links:

<?php
$terms = get_terms( 'product_cat', array(
    'hide_empty' => true,
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
}
?>

This allows customers to navigate directly to specific product categories, improving user experience and encouraging exploration.

Using WooCommerce REST API

For external applications or headless CMS setups, the WooCommerce REST API offers an efficient way to get product categories. You can use a GET request to the following endpoint:

GET /wp-json/wc/v3/products/categories

You will need Consumer Key and Consumer Secret to authenticate your request. The API returns JSON-formatted data containing all the product categories.

This is ideal for mobile apps, Progressive Web Apps (PWAs), or third-party integrations needing real-time access to your category data.

Tips for Better Category Management

  • Use Clear Naming Conventions: Avoid vague or duplicate names to prevent confusion.
  • Maintain a Clear Hierarchy: Organize parent and child categories logically.
  • SEO Optimization: Use keyword-rich names and descriptions for better search visibility.

Conclusion

Knowing how to retrieve WooCommerce categories is essential whether you’re maintaining a simple online store or developing a complex eCommerce solution. From using the WordPress admin interface to applying powerful programming functions and APIs, WooCommerce equips you with all the tools necessary to manage product categorization effectively.

Effective category management not only facilitates better user navigation but also contributes to a more organized and SEO-friendly online store. By mastering the methods outlined above, you ensure that your WooCommerce shop remains scalable, user-centric, and easy to maintain.

To top