I want to add a back button to the Single Product page so that the page will be redirected to the relevant parent category page of the product upon clicking. I have managed to get this done for one category using the following code. Here it will redirect the page to the shirts category upon clicking the back to designs button.
add_action( 'woocommerce_before_single_product', 'content_before_addtocart_button' );
function content_before_addtocart_button() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
if( $term->slug === 'shirts')
echo '<div class="content-section"><a href="' . esc_url( get_term_link( $term->term_id, 'product_cat' ) ) . '">' . 'Back to Other Designs'. '</a></div>';
}
}
I want to modify this so that it will get the current category page link of the product automatically without adding them manually like this and redirect the page accordingly. Any help on this is highly appreciated. Thanks in advance.