Add a custom button for a specific product categor

2019-06-02 06:44发布

I want to display an additional button below the description to a specific product category: "bracelets" so I developed a piece of code, which does not work:

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

function my_extra_button_on_product_page() {
   if ( is_product_category('bracelets')) {
     global $product;
     echo '<a href="www.test.com">Extra Button</a>';
   }
}

Any idea about what's wrong here?

3条回答
三岁会撩人
2楼-- · 2019-06-02 06:50

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>';
    }
}
查看更多
老娘就宠你
3楼-- · 2019-06-02 07:03
add_action( 'woocommerce_after_single_product_summary', 'my_extra_button_on_product_page', 30 );
查看更多
再贱就再见
4楼-- · 2019-06-02 07:11

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().

查看更多
登录 后发表回答