WordPress – Adding a CTA on product page which goes to the main category of this same product

  php, wordpress, yoast

I am working to make it easier for our web visitors to go to the main category of a product they’re checking. Besides the breadcrumbs I want to add this CTA somewhere at the bottom of the product page. My intention is when people are done scrolling and are interested in seeing more of this product series they can use this CTA and go to an overview page of this series.

I have something that is working. However, the code is creating a URL that is different from what the entire website is using. Different as in it adds the category that is above the main category.
To give you an example:

My site is using a structure in the navigation and internal linking like my example below:

The script for having CTA’s on these pages are creating a URL similar to:

Below you can find the script, which is added the the functions.php of my site:

add_action( 'woocommerce_after_single_product_summary', 'primaryCategoryCta', 10 );
function primaryCategoryCta() { 
        $primary_term_product_id = yoast_get_primary_term_id('product_cat');
        $postProductTerm = get_term( $primary_term_product_id );
      echo'<div class="primaryCategoryCta">';
      echo'<p>Check all the products from series ';
      echo $postProductTerm->name;
      echo '<a href="' .  esc_url( get_term_link( $postProductTerm->term_id ) ) . '">';
      echo 'Click here';
      echo '</a>';

It would be great if the URLs that are being created only contain the primary category. Other wise Google might see this URLs as duplicate.

Thank you very much in advance!

Source: Ask PHP