简体   繁体   English

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

[英]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.

相关问题 向 WooCommerce 单个产品页面添加多个选项卡 - Adding multiple tabs to WooCommerce single product pages 仅在 WooCommerce 单个产品页面中隐藏某些产品的选项卡 - Hiding tabs only for some products in WooCommerce single product pages 在 WooCommerce 特定 ID 产品页面上的多个产品中添加自定义内容 - Add custom content into multiple products on WooCommerce specific IDs product pages 使用 Avada 删除特定产品的 WooCommerce 产品选项卡 - Remove WooCommerce product tabs for specific products with Avada 在WooCommerce的单个帖子中获取产品类别的完整产品单页 - Get full products single pages for a product category on a single post in WooCommerce Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce 更改 WooCommerce 单个产品页面中的副标题“相关产品” - Change subtitle “Related Products” in WooCommerce single product pages 在WooCommerce可变产品单页中获取选定的产品变体ID - Get the selected product variation ID in WooCommerce variable products single pages 在单个产品页面中显示特定产品属性值的相关产品 - Display Related products for a specific product attribute value in single product pages 将简短描述移动到 Woocommerce 单个产品页面中的选项卡中 - Move short description into tabs in Woocommerce single product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM