Get only relevant sub-category of category “Genre” that applies to current product only

Question

As the title suggests, I am trying to get the sub-category of the Genre category.

My WooCommerce category structure looks like this for example:

Genre, Genre > Rock, Artist, Artist > Abigail Jay > Hulking Depend Weigh, Album > Hulking Depend Weigh, Year, Year > 2014

I know what my product/song ID is.
I know what the Genre category ID is.

I struggle with the following:

global $product;
$terms = get_the_terms($product->get_id(), 'product_cat');
foreach($terms as $term)
{
    if($term->slug == "genre")
    {
        $_id = $term->term_id;
        $_s = $term->slug;
        $_ar = array('parent' => $_id);
        $assoc_categories = get_terms('product_cat', $_ar);
    }
}

When running print_r($assoc_categories); of the above, it simply returns an array with all the sub-categories of parent “Genre”. This is however not right as I need to get the name of only the relevant sub-category associated with the specific product/song.

Say for example, the genre of the song is “Rock”. Rock is a sub-category of the “Genre” category. The sub-category Rock is associated with the product/song name.

How can I access only the relevant sub-category of Genre in this instance?

I am struggling a bit to find answers online, unless I keep looking for the wrong solution, by searching the wrong terms…

Any help would be greatly appreciated!

Please do keep in mind that I am still learning about coding with WordPress

0
, rdk 1 year 2020-07-13T12:10:20-05:00 0 Answers 46 views 0

Leave an answer

Browse
Browse