I want to customize WooCommerce category page (archive-product.php) to show current product category, but also I want to create Previous & Next links to previous and next category term links somewhere in the page.
I've tried a lot of solutions with no luck. I've manage to implement a code that take actual category ID, I've added +1 and -1 and I was able to create the links but the big issue is when 'next link' is on the last category that is not working, is not infinite loop for the links. I.E if I'm on cat page 6 that is last category I have, next one should be 1 and prev one should be 5 and if I'm on first category, prev one should be 6 and next one should be 2.
Please find below the piece of code I`m using and is working but not as I need:
<?php
$taxonomy = 'product_cat';
$cate = get_queried_object();
$cateID = $cate->term_id;
$next_cateID = $cateID + 1;
$prev_cateID = $cateID - 1;
$next_term_link = get_term_link( $next_cateID, $taxonomy );
$next_term = get_term( $next_cateID, $taxonomy );
$next_slug = $next_term->name;
$prev_term_link = get_term_link( $prev_cateID, $taxonomy );
$prev_term = get_term( $prev_cateID, $taxonomy );
$prev_slug = $prev_term->name;
?>
<p><a href="<?php echo $prev_term_link ?>"><?php echo $prev_slug; ?></a></p>
<p><a href="<?php echo $next_term_link ?>"><?php echo $next_slug; ?></a></p>