questions

Woocommerce add_link_to_product_page – for a specific category only – WordPress

Rate this post

i’ve inherited a site & someone has created a plugin to add a subscribe button to single products, which simply links to a separate page. it’s currently appearing on all products, however the client only wants this button/link to appear on products belonging to a specific category. i know this is a simple change, for anyone but me. can anyone point me in the right direction here? product-category/wine or, taxonomy=product_cat&tag_ID=45 here’s the code:

function add_link_to_product_page(  ) { 
    global $product;
    if( !$product->is_type( 'grouped' ) ) {
        echo'<a class="button single_add_to_cart_button" href="' . get_permalink(1073) . '">Subscribe</a>';
    }
}

add_action( 'woocommerce_after_add_to_cart_form', 'add_link_to_product_page', 10, 0 ); 
?>

 

✔️Solution:

You can use has_term() function. please check below code.

function add_link_to_product_page() {
    global $product;
    if( !$product->is_type( 'grouped' ) && has_term( 45, 'product_cat' ) ) {
        echo'<a class="button single_add_to_cart_button" href="' . get_permalink(1073) . '">Subscribe</a>';
    }
}
add_action( 'woocommerce_after_add_to_cart_form', 'add_link_to_product_page', 10, 0 ); 

Leave a Reply

Your email address will not be published.

Back to top button