Unable to get category object in some WooCommerce templates.

I use get_category()to get a category object by its identifier. Here is a child . For instance, category 39 category 37

Courses (37) 
    - Programming (39)

My problem is that if I use get_category(37 or 39)in functions.php, BOTH will return null. If I use get_category(37 or 39)in single-product.php37 (the root category) then null is returned. If I use the same call in add-to-cart/simple.php, both return an object.

The WooCommerce functions will be called first, and then single-product.php, and then the add-to-cart/simple.phptemplates.

What's happening?
Why does this work depending on the file?


@edit

get_term( 37, 'category' ); 

doesn't seem to work either


@edit 13/7 - The right working solution

, :

$category = get_term_by('id', $category_id, 'product_cat');

:

+4
1

get_category() get_term() . , (. ). , ( ).

, slug, get_category_by_slug('the_slug'). :

$idObj = get_category_by_slug('my_category_slug'); 
$id = $idObj->term_id;

WordPress:
, get_the_category_by_ID().
, get_cat_ID( 'cat_name' ).


get_category() ():

, , ( )

function products_cats_subcats(){
    $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 );
    echo '<ul>';
    foreach ($all_categories as $cat) {
        if($cat->category_parent == 0) {
            $category_id = $cat->term_id;
            echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';

            $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 );
            echo '<ol>';
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo  '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">' . $sub_category->name .'</a></li>';
                }
            }
            echo '</ol>';
        }
    }
    echo '</ul>';
}

, , : <?php products_cats_subcats() ;?>

, , .


get_term_by() :

$term = get_term_by('id', $term_id, 'product_cat', 'ARRAY_A');

$term['name']; //get the WC category name
$term['slug']; //get the WC category slug

, ...


:

+4

All Articles