0

What are other methods to get ID of category by name.

I have loop and need to check if category exist if it is then return category ID else make new one.

This is the function what check for the category ID in woocommerce and it was working fine, but not anymore.

    function get_product_category_id($product_name){

    $product_cat = get_term_by( 'name', $product_name, 'product_cat' );

    if ( $product_cat ) {
        return $product_cat->term_id;
    }
    return 0;
  }

This function works if i use with Wordpress post categories but not anymore with Woocommerce categories.

And now my function to assigning category to product is not working, because i cant get woocommerce category id...

wp_set_object_terms($product_id, $cat_id, 'product_cat');
Danijel
  • 47
  • 5

0 Answers0