'Show list of tags if product if has tag and product attribute

In Woocommerce, I have products with tags (jeans, trousers, sweater etc) and specific attributes (pa_size = small, pa_size = medium, pa_size = large). On a page, I only want to list the tags if the product has size M.

How can I do this? I started with this, but have no clue how to list the tags. I am still learing php and logical thinking but have a long way to go.

<?php 

$terms = get_terms("pa_size");
foreach ( $terms as $term ) {

// this does not work, but I need to check if the pa_size = medium
if ($terms == "Medium"){ 

// if is Medium, then find out how to display all tags

}}

?>


Sources

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

Source: Stack Overflow

Solution Source