简体   繁体   中英

How to add a link on woocommerce product page as per the product category?

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>';
}

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>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM