Skip to content

Instantly share code, notes, and snippets.

@shubhw12
Created July 22, 2020 07:20
Show Gist options
  • Save shubhw12/fa3d5df0e49799fa66a339ab7db7613b to your computer and use it in GitHub Desktop.
Save shubhw12/fa3d5df0e49799fa66a339ab7db7613b to your computer and use it in GitHub Desktop.
Add back to category button at the end of every single product
add_action ('woocommerce_after_single_product','add_back_product_category_button', 5);
function add_back_product_category_button(){
// Get the product categories set in the product
$terms = wp_get_post_terms( get_the_id(), 'product_cat' );
// Check that there is at leat one product category set for the product
if(sizeof($terms) > 0){
// Get the first product category WP_Term object
$term = reset($terms);
// Get the term link (button link)
$link = get_term_link( $term, 'product_cat' );
// Button text
$text = __('Back to','woocommerce') . ' <strong>' . $term->name . '</strong>';
// Output
echo '<p><a href="'.esc_url($link).'" title="'.$text.'" class="button '.$term->slug.'">'.$text.'</a></p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment