将自定义选项卡添加到 WooCommerce 中特定产品类型的单个产品页面
Adding custom tab to single product page for specific product type in WooCommerce
基于此代码,我想在 WooCommerce 中为可变产品创建自定义选项卡 4.4.1。
但是不幸的是,这个自定义选项卡被添加到所有产品类型中,请问有什么方法可以解决这个问题吗?
如有错误请指正
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab for variable product type
global $product;
if( $product->is_type( 'variable' ) ) {
$tabs['test_tab'] = array(
'title' => 'features',
'priority' => 50,
'class' => array('general_tab', 'show_if_variable'),
'callback' => 'woo_new_product_tab_content'
);
}
return $tabs;
}
要查找错误,您可以执行一些额外的检查并打印产品类型。
else
条件测试后可以去掉
function filter_woocommerce_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get type
$product_type = $product->get_type();
// Compare
if ( $product_type == 'variable' ) {
$tabs['test_tab'] = array(
'title' => 'features',
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
} else {
echo 'DEBUG: ' . $product_type;
}
} else {
echo 'NOT a WC product';
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 10, 1 );
// Callback
function woo_new_product_tab_content() {
// The new tab content
echo '<h2>New Product Tab</h2>';
echo '<p>Here\'s your new product tab.</p>';
}
基于此代码,我想在 WooCommerce 中为可变产品创建自定义选项卡 4.4.1。
但是不幸的是,这个自定义选项卡被添加到所有产品类型中,请问有什么方法可以解决这个问题吗?
如有错误请指正
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab for variable product type
global $product;
if( $product->is_type( 'variable' ) ) {
$tabs['test_tab'] = array(
'title' => 'features',
'priority' => 50,
'class' => array('general_tab', 'show_if_variable'),
'callback' => 'woo_new_product_tab_content'
);
}
return $tabs;
}
要查找错误,您可以执行一些额外的检查并打印产品类型。
else
条件测试后可以去掉
function filter_woocommerce_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get type
$product_type = $product->get_type();
// Compare
if ( $product_type == 'variable' ) {
$tabs['test_tab'] = array(
'title' => 'features',
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
} else {
echo 'DEBUG: ' . $product_type;
}
} else {
echo 'NOT a WC product';
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 10, 1 );
// Callback
function woo_new_product_tab_content() {
// The new tab content
echo '<h2>New Product Tab</h2>';
echo '<p>Here\'s your new product tab.</p>';
}