[英]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 caseshas_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.