php - 将特定产品的多个选项卡添加到 WooCommerce 单个产品页面
问题描述
我正在尝试在特定产品上显示额外的产品标签,$post_id
但我不确定它是否正确?这种方式适用于我写的其他小片段。
如果我让它们显示在所有产品上,产品标签就可以工作,但我想将一些限制在特定产品上。
我的代码尝试:
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;
}
任何帮助表示赞赏!
解决方案
$post_id
不会传递给woocommerce_product_tabs
过滤器钩子。
您可以使用global $product
&$product->get_id()
代替。
所以你得到:
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>';
}
推荐阅读
- css - 如何使 div 高度填充剩余空间,但不随着内容高度的增加而扩展?
- flutter - 如何在 Flutter 中制作“Cartogram”
- python - 无法使用 pyaudio 正确读取麦克风输入
- entity-framework-core - Entity Framework Core:带有顺序的多对多表
- javascript - 将 infinispan js 客户端连接到需要身份验证的服务器
- kivy - 从 Kivy Recycleview 检索数据
- python - 从 Python 中的列表中查找所有可能的“真实”单词组合,最多 x 个单词
- java - 如何将实用程序.java 作为文件夹放入库中,以便可以将其方法用于多个 java 项目?
- java - Java 反应式。如何等待所有不断变化的数据然后处理它们
- php - 为什么我不能在 Laravel 的 Clouse 中使用列别名?