Add a custom button for a specific product category in Woocommerce

浪尽此生 提交于 2019-12-22 18:44:15

问题


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?


回答1:


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




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/48471999/add-a-custom-button-for-a-specific-product-category-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!