The true solution:
$catslug = get_query_var( 'product_cat' );
if(get_field('eref-' . $catslug . ''))
{
echo '<div><span>REF: ' . get_field('eref-' . $catslug . '') . '</span></div>';
}
I hope it can help someone else in the future too!
<?php echo single_term_title(); ?>
When placed outside or inside of the product archive loop correctly returns the proper title of the active Woocommerce category. So now I just need a way to break that result down to the slug, and I’d be in business.
Damn.
I spoke too soon.
The two categories I was testing did not contain any products which are located in multiple categories. If a product is located in multiple categories (with a REF letter set for multiple categories) then the PHP is loading the REF from the first category in the array. I added echo '<div>eref-' . $categories[0]->slug . '</div>';
into the loop to confirm that is what was happening. See the image below:
Solved. Thanks for your help John.
My final code on content-product.php
$categories = get_the_terms($post_id, 'product_cat');
if(get_field('eref-' . $categories[0]->slug . ''))
{
echo '<div><span>REF: ' . get_field('eref-' . $categories[0]->slug . '') . '</span></div>';
}
$categories = get_the_terms($post_id, 'product_cat');
echo $categories;
The above returns “Array”
Added echo 'eref-' . $category[0]->slug . '';
to my content-product.php that drives the Woocommerce category archive loop, and it just returned “eref-“.
So I guess ' . $category[0]->slug . '
is not matching up to anything.