Get Woocommerce Product Categories from Wordpress

Get WooCommerce product categories from WordPress

<?php

$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
?>

This will list all the top level categories and subcategories under them hierarchically. do not use the inner query if you just want to display the top level categories. Style it as you like.

Get all product categories in WooCommerce

Product category is a "custom taxonomy" product_cat used by WooCommerce products.

You need to use get_terms(), with the correct taxonomy, this way:

// Get Woocommerce product categories WP_Term objects
$categories = get_terms( ['taxonomy' => 'product_cat'] );

// Getting a visual raw output
echo '<pre>'; print_r( $categories ); echo '</pre>';

You can also get empty product categories using get_terms() like:

$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );

Tested and works (WordPress 3.5+ and WooCommerce 2.4+)… Both should works for you.

You will get something like:

Array
(
[0] => WP_Term Object
(
[term_id] => 83
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 83
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 5
[filter] => raw
[meta_value] => 1
)

[2] => WP_Term Object
(
[term_id] => 11
[name] => Tshirts
[slug] => tshirts
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
[meta_value] => 2
)
// … and so on …
)

How to get Woocommerce Top level product categories?

You just need the following arguments in get_terms() function:

$terms = get_terms( array('taxonomy' => 'product_cat', 'parent' => 0) );

foreach ( $terms as $term ){
$term_link = get_term_link( $term, $taxonomy );

echo '<a class="ccats" href="'.$term_link.'"><span class="label">'.$term->name.'</span></a>';
}

Here are all available arguments that can be used in get_terms() function.


For product category image: Get and display the product category featured image in Woocommerce

WooCommerce - get category for product page

A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}

Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>

How to get Woocommerce Product Categories within a Plugin File

Finally I could solve this with MySQL Query here I m trying to get Woocommerce Product Categories in a WP AJAX function where get_categories() or get_terms() does not work for product_cat taxonomy. But within this function I can execute WP MySQL Quires so the following code produces and returns the expected output.

add_action( 'wp_ajax_tcf_et_mp_get_categories', 'tcf_et_mp_get_categories' );
function tcf_et_mp_get_categories()
{
global $wpdb;
$categories = $wpdb->get_results( " SELECT wp_terms.*
FROM wp_terms
LEFT JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'product_cat'" );

$materials_drop_down = '<select>';
$materials_drop_down .= '<option value="0">Choose Material Type</option>';
foreach( $categories as $category )
{
$materials_drop_down .= '<option value="'. $category->term_id .'">' . $category->name . '</option>';
}
$materials_drop_down = '</select>';

wp_send_json( $materials_drop_down );
die;
}

Hope this will help for someone.

Get WooCommerce mixed 10 products from multiple product categories

Updated

Your code is a bit outdated, instead try the following to query products from specific product category terms using the argument "orderby" set to "rand" (random order by) to make them mixed up:

$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 10,
'post_status' => 'publish',
'orderby' => 'rand',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('ring', 'watch', 'earring'),
) ),
) );

Tested and work.

Displaying product category names in the WordPress order table (Woocommerce)

$items = $order->get_items();

$cats = '';

foreach($items as $key => $item) {

$product_id = $item['product_id'];
$term_obj_list = get_the_terms($product_id, 'product_cat');
$terms_string = implode(', ', wp_list_pluck($term_obj_list, 'name'));

$cats .= $terms_string;
}

echo $cats;

Put this in correct elseif block of your given code.

Updated according to the comment with code comment:

//Getting all the items in the order
$items = $order->get_items();

$c_list = [];

/**
* Loop through all items and get their categories [a product can have multiple category]
*/
foreach($items as $key => $item) {

//We need the product id to retrieved the categories
$product_id = $item['product_id'];

//Retrieving all the categories that product has
$term_obj_list = get_the_terms($product_id, 'product_cat');


//Loop through all the categories the current product has and saving it in a array with category id as index
//so that if the save category is assigned to another product we do not get the duplicate category
foreach($term_obj_list as $term_obj) {
$c_list[$term_obj->term_id] = $term_obj->name;
}
}

//lastly we concatenating all the categories with ", "
$cats = implode(', ', $c_list);

//printing the final result
echo $cats;


Related Topics



Leave a reply



Submit