将特定产品的多个选项卡添加到 WooCommerce 单个产品页面

Adding multiple tabs for specific products to WooCommerce single product pages

我正在尝试使用 $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>';
}