Reputation: 5
I was able to find a code to add a link on woocommerce product page but I want to add a link as per the product category. Following is the code that I added to display the link on the product page. Can someone please help me get the code to add the link to the product page as per the product category?
add_action( 'woocommerce_before_add_to_cart_button',
'content_before_addtocart_button' );
function content_before_addtocart_button() {
echo '<div class="content-section">add custom size <a
href="http://google.com">here</a></div>';
}
Upvotes: 0
Views: 4645
Reputation: 4243
Place this code in your functions.php file it will output a link if the category slug matches 'jackets' in this instance.
add_action( 'woocommerce_before_add_to_cart_button', '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 === 'jackets')
echo '<div class="content-section">add custom size <a href="' . esc_url( get_term_link( $term->term_id, 'product_cat' ) ) . '">' . $term->name . '</a></div>';
}
}
Upvotes: 1