[英]Adding multiple tabs for specific products to WooCommerce single product pages
I'm trying to show extra product tabs on specific products using $post_id
I'm not sure if it's done it right?我正在尝试使用
$post_id
在特定产品上显示额外的产品标签 我不确定它是否做得对? this way works for other small snippets i've wrote.这种方式适用于我写的其他小片段。
The product tabs work if i have them display on all product, but i want to limit some to specific products.如果我让它们显示在所有产品上,产品选项卡就可以工作,但我想将一些限制在特定产品上。
My code attempt:我的代码尝试:
add_filter( 'woocommerce_product_tabs', 'artwork_product_tab' );
function artwork_product_tab( $tabs, $post_id ) {
if( in_array( $post_id, array( 8125 ) ) ){
// Adds the new tab
return $tabs['artwork_guidelines'] = array(
'title' => __( 'Artwork Guidelines', 'woocommerce' ),
'priority' => 50,
'callback' => 'artwork_product_tab_content'
);
}
$tabs['standard_sizes'] = array(
'title' => __( 'Standard Sizes', 'woocommerce' ),
'priority' => 60,
'callback' => 'standard_sizes_product_tab_content'
);
return $tabs;
}
Any help is appreciated!任何帮助表示赞赏!
$post_id
is not passed to the woocommerce_product_tabs
filter hook. $post_id
未传递给woocommerce_product_tabs
过滤器挂钩。
You can use global $product
& $product->get_id()
instead.您可以改用
global $product
& $product->get_id()
。
So you get:所以你得到:
function filter_woocommerce_product_tabs( $tabs ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get product ID
$product_id = $product->get_id();
// Compare
if ( in_array( $product_id, array( 8125, 30, 815 ) ) ) {
$tabs['artwork_guidelines'] = array(
'title' => __( 'Artwork Guidelines', 'woocommerce' ),
'priority' => 50,
'callback' => 'artwork_product_tab_content'
);
$tabs['standard_sizes'] = array(
'title' => __( 'Standard Sizes', 'woocommerce' ),
'priority' => 60,
'callback' => 'standard_sizes_product_tab_content'
);
}
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
// New Tab contents
function artwork_product_tab_content() {
echo '<p>artwork_product_tab_content</p>';
}
function standard_sizes_product_tab_content() {
echo '<p>standard_sizes_product_tab_content</p>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.