Get category id by product in WooCommerce

Get category id by product in WooCommerce

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Product Category IDs in WooCommerce Orders

When building logic to validate or route orders based on product categories, it is common to iterate through order line items and extract taxonomy data. The scenario often looks like this: you have an order object, you retrieve its items via get_items(), and then you attempt to fetch the category ID for each product associated with that item.

If you try to use $item['category_id'] directly, you will encounter a runtime error or simply receive nothing, because line item objects do not store category IDs by default. The data exists in the database, but it is not exposed as a direct property on the item object. This forces developers to query the WordPress taxonomy system manually using the product ID found within the item.

The most straightforward approach involves leveraging the global get_the_terms() function. This function accepts a post ID and a taxonomy name (in this case, 'product_cat') and returns an array of term objects. You can then iterate over that result to extract the numeric ID.

Here is a practical implementation pattern:

$order = new WC_Order( $order_id );
$items = $order->get_items();

$found_category_ids = [];

foreach ( $items as $item ) {
    // Get the product ID associated with this line item
    $product_id = $item['product_id'];

    // Fetch terms for the 'product_cat' taxonomy. 
    // Note: The second argument must be the taxonomy slug, not 'term_id'.
    $terms = get_the_terms( $product_id, 'product_cat' );

    if ( is_wp_error( $terms ) || empty( $terms ) ) {
        continue;
    }

    // Loop through returned terms to extract IDs
    foreach ( $terms as $term ) {
        $found_category_ids[] = $term->term_id;
    }
}

A critical detail in your original attempt was the second argument passed to get_the_terms(). The function signature expects ( $post_id, $taxonomy ). In your code, you passed 'term_id' as the taxonomy name. Because no taxonomy is actually registered with the slug term_id, WordPress returned a WP_Error object with the message "Ongeldige taxonomie" (Invalid taxonomy).

The correct taxonomy slug for WooCommerce product categories is 'product_cat'. Alternatively, if your site uses custom categories or a different taxonomy structure, ensure you replace 'product_cat' with the specific handle of that taxonomy.

Handling Product Variations

There is another nuance to consider when working with variable products. If a line item represents a variation rather than the parent product, the get_the_terms() function might return empty results or unexpected data depending on how the variation is linked in the database.

Variations usually inherit category relationships from their parent product, but the term data is sometimes stored differently. If you encounter an empty array after calling get_the_terms(), it is often safer to check if the item type is a variation and potentially query the parent product ID instead. You can check this using $item->variation_id or by inspecting the $product object obtained via wc_get_product( $product_id ).

Performance Considerations

If you are processing a large order with many items, calling get_the_terms() inside the loop is functional but not optimal. This function performs a database query for every single item. If you have an order with 50 line items, that is potentially 50 separate database calls just to fetch category data.

For high-volume scenarios or when processing multiple orders in a background job, consider batching these requests. You can collect all unique product IDs from the items first, then call get_the_terms() once per unique product ID, storing the results in an associative array for quick lookup. This reduces the total number of database queries significantly.

Additionally, always check if $terms is a WP_Error or an empty array before accessing properties like term_id. Failing to do so can lead to PHP notices or silent failures in your validation logic.

Our network