Add a custom button for a specific product category in Woocommerce

懵懂的女人 提交于 2019-12-06 12:23:29

Your question is not really clear.

1) If you want to display a custom button for a specific product category on product category archives pages below the description of this product category you will use:

add_action( 'woocommerce_archive_description', 'extra_button_on_product_category_archives', 20 );
function extra_button_on_product_category_archives() {
    if ( is_product_category('bracelets') ) {
        echo '<a class="button" href="www.test.com">Extra Button</a>';
    }
}

2) If you want to display a custom button in single product pages for a specific product category below the short description of this product you will use:

add_action( 'woocommerce_single_product_summary', 'extra_button_on_product_page', 22 );
function extra_button_on_product_page() {
    global $post, $product;
    if ( has_term( 'bracelets', 'product_cat' ) ) {
        echo '<a class="button" href="www.test.com">Extra Button</a>';
    }
}

3) If you want to display a custom button in single product pages for a specific product category below the description (in the product tab) of this product you will use:

add_filter( 'the_content', 'add_button_to_product_content', 20, 1 );
function add_button_to_product_content( $content ) {
    global $post;

    if ( is_product() && has_term( 'bracelets', 'product_cat' ) )
        $content .= '<a class="button" href="www.test.com">Extra Button</a>';

    // Returns the content.
    return $content;
}

4) If you want to display a custom button in single product pages for a specific product category below the product tabs, you will use:

add_action( 'woocommerce_after_single_product_summary', 'extra_button_on_product_page', 12 );
function extra_button_on_product_page() {
    global $post, $product;
    if ( has_term( 'bracelets', 'product_cat' ) ) {
        echo '<a class="button" href="www.test.com">Extra Button</a>';
    }
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works.

For product category archives pages use is_product_category().
For all other cases has_term().

add_action( 'woocommerce_after_single_product_summary', 'my_extra_button_on_product_page', 30 );

the following code adds the button before the loop on a product category archive

add_action( 'woocommerce_archive_description', 'extra_button_on_product_category_archives', 20 );
function extra_button_on_product_category_archives() {
    if ( is_product_category('bracelets') ) {
        echo '<a class="button" href="www.test.com">Extra Button</a>';
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!