'Adding Taxonomy Name above Woocommerce Product Loop Title

I'm trying to add extra information to the standard Woocommerce product archive loop widget. On some pages I want to show a specific meta tag above the product title. On other pages I want to show the term(s) of a custom taxonomy.

For the meta tag, I got it to work. The name of the meta tag shows above the product title. Great! (See this image)

However, for custom taxonomies it's not working.

I have a taxonomy 'country' for example. When visitors go to the url of the red wine archive "/product-categorie/rode-wijn/", I want the country of the wine to be above the title.

Here's the code I have so far:

add_action( 'woocommerce_before_shop_loop_item_title', 'add_meta_above_title', 10);
    
function add_meta_above_title() {
global $product;

// Getting relevant data from meta_fields and taxonomies.
$winetype = get_post_meta( $product->id, 'wine_type', true );   
$country = get_the_terms( $product->id, 'country', array( 'fields' => 'names' ) );

// If front page, shop page or product page, 
//add meta tag 'winetype' before product title
    if (is_front_page() || is_shop() || is_product())
        echo '<div class="product-meta-above-title">' . ucwords( $winetype ) . '</div>';

// If URL contains '/winery/', add meta tag 'winetype' before product title
    if (strpos($_SERVER['REQUEST_URI'], "/winery/") !== false)
        echo '<div class="product-meta-above-title">' . ucwords( $winetype ) . '</div>';

// If the URL is "/product-categorie/rode-wijn/" , add taxonomy terms from custom taxonomy
//'country' before product title
    if (strpos($_SERVER['REQUEST_URI'], "/product-categorie/rode-wijn/") !== false) {
        if ((empty($country)))
            return;
        foreach ($country as $current) {
            echo '<div class="product-meta-above-title">' . ucwords( $current ) . '</div>';
            }
        }
    }

The taxonomy is checked in the backend: see image
The div get's added if the 'country' array contains items: See image

Is there anyone that might spot an error in my code?

Any help would be awesome :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source