Get all product categories in WooCommerce
Asked Answered
B

2

13

I am trying to get product categories in WooCommerce, but get_terms() function doesn't work for me. I am getting an empty array.

What I am doing wrong? How to get all Woocommerce product category terms?

Beeeater answered 12/4, 2019 at 11:37 Comment(0)
A
23

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 …
)
Autogiro answered 12/4, 2019 at 11:43 Comment(3)
Not working. I am getting this WP_Error Object ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy. ) ) [error_data] => Array ( ) )Beeeater
Sorry but this code is tested and works… the taxonomy for woocommerce categories is "product_cat'… So you are making some confusions, somewhere.Autogiro
You should be using get_the_terms with a check for empty $termsNonexistence
H
0

I got mine out of all product categories, also exclude categories that I don't want to display

$taxonomy = "product_cat";
$terms = get_terms($taxonomy, array('orderby' => 'slug', 'hide_empty' => false, 'exclude' => array( 19 ))); //Exclude Specific Category by ID

foreach ($terms as $term) {
    $thumbnail_id = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
    $image = wp_get_attachment_url($thumbnail_id); ?>

    <div class="col-12 col-md-3">
        <div class="cat-item drop-shadow white-b padding-20 align-center margin-bottom-30">
            <?php 
                echo '<h3 class="uppercase strong blue-2-c margin-bototm-20 equal-height">' . $term->name . '</h3>';
                echo '<div class="item-thumbnail" style="background: url('.$image.');height:150px"></div>'; 
                echo '<a href="' . get_term_link($term->name, $taxonomy) . '" class="button color-blue-2-radial">View Range</a>';
            ?>
        </div>
    </div>
Hydric answered 10/2, 2022 at 17:3 Comment(1)
get_woocommerce_term_meta is deprecatedNonexistence

© 2022 - 2024 — McMap. All rights reserved.