在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>';
}
}
有什么想法吗?
推荐答案
您的问题不是很清楚。
1)如果要显示在该产品类别说明下方的产品类别档案页面上,针对特定产品类别的自定义按钮,您将使用:
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)如果要在显示自定义按钮特定产品类别的单个产品页面,将在该产品的简短描述下方使用:
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)如果要在其中显示自定义按钮此产品的说明(在产品标签中)下方的特定产品类别的单个产品页面,您将使用:
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)如果要在单个产品页面中显示自定义按钮产品标签下方的特定产品类别,您将使用:
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>';
}
}
代码进入function.php文件您的活动子主题(或活动主题)。
经过测试并有效。
对于产品类别归档页面,请使用
is_product_category()
。
对于所有其他情况,has_term()
。
这篇关于在Woocommerce中为特定产品类别添加自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!