0
0
001alena0012020-06-03 17:58:46
WordPress
001alena001, 2020-06-03 17:58:46

How to display product category id in woocommerce loop?

there is a code:

<?php global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) { 
$_product =  wc_get_product( $values['data']->get_id()); 
echo  wc_get_product_category_list( $values['data']->get_id());
}?>

It displays the products that are in the basket, removed unnecessary arguments, you need to display the data of the product category. The function "wc_get_product_category_list" displays categories with links without slug and id, how can I enter id or slug of the categories to which the product belongs in the loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Sedov, 2020-06-03
@001alena001

Displays the id of the product category

$product_cats_ids = wc_get_product_term_ids($values['product_id'], 'product_cat');
echo $product_cats_ids[0];

Displays the product category slug
$terms = get_the_terms($values['product_id'], 'product_cat');
echo $terms[0]->slug;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question