Limit {post_terms_product_cat} in woocommerce products query to lowest level

I have a custom Woocommerce Archive template using the Products widget. Inside I have a field to show category for each product.

I am using {post_terms_product_cat} to show but it’s showing all 3 category levels. I only want to show the lowest level category but can’t figure out how to do that.

For anyone else wondering, I modified the code from this doc for the product_first_category_name() function:

function product_first_category_link()
{

	global $product;

	if ($product && is_a($product, 'WC_Product')) {

		$terms = get_the_terms($product->get_id(), 'product_cat');

		if (!empty($terms)) {

			$first_term = $terms[0];

			return '<a href="' . get_term_link($first_term) . '">' . $first_term->name . '</a>';
		}
	}
}