How to get product category ids from order items in WooCommerce

D p :

I am working on to get the categories of each product which is ordered using below code.

function get_order_detail($order_id){
    $order = wc_get_order( $order_id );


    foreach ($order->get_items() as $item_key => $item ){
        $product = $item->get_product();
        $categorieID = $product->category_ids[0];
        $categorie_title = get_the_category_by_ID($categorieID);
    }
}

But the products which were having variants like size, colour in their variations, they are returning the value of $categorieID as NULL.

LoicTheAztec :

Use the following instead to get the product category term names for each order item:

function get_order_detail( $order_id ) {
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item_id => $item ) { 
        // Get the product category(ies) term(s) name(s) - (array)
        $term_names = wp_get_post_terms( $item->get_product_id(), 'product_cat', array('fields' => 'names') );
        // Set them as a coma separated string
        $categories_string = implode(',', $term_names);

    }
}

or you can get the product category Ids using the following:

function get_order_detail( $order_id ) {
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item_id => $item ) { 
        // Get the product category(ies) term(s) Id(s) - (array)
        $term_ids = wp_get_post_terms( $item->get_product_id(), 'product_cat', array('fields' => 'ids') );
    }
}

This will work for all product types even variations of a variable product.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get product categories ids from order items in WooCommerce

How to get the product sku from order items in Woocommerce

Send email if woocommerce order has items from a specific product category

Get a list of siblings term ids from current product category in WooCommerce

Get the product category terms from cart items in WooCommerce

How to get order items ids to get some product meta data?

Get the parent product categories ids from a sub product category ID in Woocommerce

WooCommerce get product variation attribute name and value from order items

How to get all products from current WooCommerce product category?

Get "primary" category image from WooCommerce product

Exclude specific product ids from a category price suffix change in WooCommerce

How to get product variation attribute slugs from Woocommerce cart items

WooCommerce: Get purchased items for a product category using a SQL query

Get WooCommerce cart items count except for a product Category

Restricting cart items to be from the same product category in WooCommerce

Set a minimum quantity for cart items from specific WooCommerce product category

Get Order items and WC_Order_Item_Product in WooCommerce 3

Woocommerce [products] shortcode - How to order by given product ids atribute?

woocommerce get product category id

Get product custom field related to Woocommerce order items

Woocommerce - remove product from category

Get variation attributes from order items in WooCommerce

Get the category name from a WooCommerce product using the product id or the category id

Checking cart items for a product category in Woocommerce

Check for Product Category in cart items with WooCommerce

How to get Category from Product? Django

Get variations IDs from a variable product in Woocommerce 3

Display product category names in Woocommerce order details

How to set category to product woocommerce