简体   繁体   English

在Woocommerce中为特定产品类别添加自定义按钮

[英]Add a custom button for a specific product category in Woocommerce

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? 您对这里出什么问题有任何想法吗?

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

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: 1)如果要在此产品类别说明下方的产品类别档案页面上显示特定产品类别的自定义按钮,请使用:

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: 2)如果您想在单个产品页面的特定产品类别下方显示此产品的简短说明下的自定义按钮,则可以使用:

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: 3)如果要在单个产品页面的特定产品类别下(在产品标签中)在特定产品类别下显示自定义按钮,您将使用:

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: 4)如果要在产品标签下方的特定产品类别的单个产品页面中显示自定义按钮,则将使用:

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). 代码进入您的活动子主题(或活动主题)的function.php文件中。

Tested and works. 经过测试和工作。

For product category archives pages use is_product_category() . 对于产品类别归档页面,请使用is_product_category()
For all other cases has_term() . 对于所有其他情况, has_term()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将自定义字段添加到特定类别的 woocommerce 产品 - Add Custom field to woocommerce product for an specific category 如何在“继续结帐”按钮上为特定产品类别添加自定义URL [WooCommerce] - How to add a custom URL on the “Proceed to checkout”, button for a specific product category [WooCommerce] 如何隐藏添加到购物车按钮特定角色和产品类别 Woocommerce - How to hide add to cart button specific role and product category Woocommerce 在WooCommerce中为特定产品类别自定义“添加到购物车”按钮 - Customize “Add to cart” button for a specific product category in WooCommerce 删除 Woocommerce 中特定产品类别的添加购物车按钮 - Remove add cart button in Woocommerce for a specific product category 为特定产品类别的 Woocommerce 产品添加自定义字段 - Add a custom field to Woocommerce products for a specific product category 产品页面上的Woocommerce类别特定按钮 - Woocommerce Category Specific Button on product page WooCommerce 特定产品类别的定制块 - WooCommerce custom slug for specific product category 输出WooCommerce中特定产品类别的自定义简码 - Output a custom shortcode for a specific product category in WooCommerce 在 Woocommerce 中为特定产品类别添加费用 - Add a fee for a specific product category in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM