php - 向 WooCommerce 单个产品页面添加多个选项卡

标签 php wordpress tabs woocommerce product

我正在尝试向 WooCommerce 添加三个自定义选项卡。我有下面的代码,其中两个出现了,但由于某种原因,属性描述选项卡没有显示在页面上。不仅数量定价选项卡不显示其描述。我尝试将不同的代码部分移动到不同的位置,并且检查了代码是否有错误或缺失部分。这是我能得到的最接近的。

我的流程是基本上删除我不想要的现有选项卡,然后按照我希望它们出现的顺序添加新选项卡。

我觉得我错过了什么。

您可以在此处查看站点:http://demo.bergdahl.com/product/6-oz-catridge/

这是我使用的代码:

// WooCommerce Tabs



// REMOVE EXISTING TABS

add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );


function woo_remove_product_tabs( $tabs ) {

    unset( $tabs['description'] );          // Remove the description tab

    // unset( $tabs['reviews'] );           // Remove the reviews tab

    unset( $tabs['additional_information'] );   // Remove the additional information tab

    return $tabs;
}




// ADD ATTRIBUTE DESCRIPTION TAB

add_filter( 'woocommerce_product_tabs', 'woo_attrib_desc_tab' );

function woo_attrib_desc_tab( $tabs ) {

    // Adds the Attribute Description tab

    $tabs['attrib_desc_tab'] = array(

        'title'     => __( 'Desc', 'woocommerce' ),

        'priority'  => 100,

        'callback'  => 'woo_attrib_desc_tab_content'

    );

    return $tabs;

}


// ADD QUANTITY PRICING TAB

add_filter( 'woocommerce_product_tabs', 'woo_qty_pricing_tab' );

function woo_qty_pricing_tab( $tabs ) {

    // Adds the qty pricing  tab

    $tabs['qty_pricing_tab'] = array(

        'title'     => __( 'Quantity Pricing', 'woocommerce' ),

        'priority'  => 110,

        'callback'  => 'woo_qty_pricing_tab_content'

    );
    return $tabs;
}

// ADD OTHER PRODUCTS TAB

add_filter( 'woocommerce_product_tabs', 'woo_other_products_tab' );

function woo_other_products_tab( $tabs ) {

    // Adds the other products tab

    $tabs['other_products_tab'] = array(

        'title'     => __( 'Other Products', 'woocommerce' ),

        'priority'  => 120,

        'callback'  => 'woo_other_products_tab_content'

    );

    return $tabs;

}

// ADD CUSTOM TAB DESCRIPTIONS

function woo_attrib_desc_tab_content() {

    // The attribute description tab content

    echo '<h2>Description</h2>';

    echo '<p>Custom description tab.</p>';

}

function woo_qty_pricing_tab_content() {

    // The qty pricing tab content

    echo '<h2>Quantity Pricing</h2>';

    echo '<p>Here\'s your quantity pricing tab.</p>';   

}

function woo_other_products_tab_content() {

    // The other products tab content

    echo '<h2>Other Products</h2>';

    echo '<p>Here\'s your other products tab.</p>';

}

根据下面 LoicTheAztec 的回复进行编辑,这是我的整个 functions.php 文件。我在底部使用和不使用 ?> 进行了尝试:

<?php
add_theme_support( 'builder-3.0' );

add_theme_support( 'builder-responsive' );



function register_my_fonts() {

    wp_register_style('googleFonts-OpenSans', '//fonts.googleapis.com/css?family=Open+Sans:400,300,700');

    wp_enqueue_style( 'googleFonts-OpenSans');

}



add_action('wp_enqueue_scripts', 'register_my_fonts');





function sc_replacecolon( $content ){ return str_replace( '[sc:', '[sc name=', $content ); }

add_filter( 'the_content', 'sc_replacecolon', 5 );



/* WOOCOMMERCE */

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs', 100, 1 );
function woo_custom_product_tabs( $tabs ) {

    // 1) Removing tabs

    unset( $tabs['description'] );              // Remove the description tab
    // unset( $tabs['reviews'] );               // Remove the reviews tab
    unset( $tabs['additional_information'] );   // Remove the additional information tab


    // 2 Adding new tabs

    //Attribute Description tab
    $tabs['attrib_desc_tab'] = array(
        'title'     => __( 'Desc', 'woocommerce' ),
        'priority'  => 100,
        'callback'  => 'woo_attrib_desc_tab_content'
    );

    // Adds the qty pricing  tab
    $tabs['qty_pricing_tab'] = array(
        'title'     => __( 'Quantity Pricing', 'woocommerce' ),
        'priority'  => 110,
        'callback'  => 'woo_qty_pricing_tab_content'
    );

    // Adds the other products tab
    $tabs['other_products_tab'] = array(
        'title'     => __( 'Other Products', 'woocommerce' ),
        'priority'  => 120,
        'callback'  => 'woo_other_products_tab_content'
    );

    return $tabs;

}

// New Tab contents

function woo_attrib_desc_tab_content() {
    // The attribute description tab content
    echo '<h2>Description</h2>';
    echo '<p>Custom description tab.</p>';
}
function woo_qty_pricing_tab_content() {
    // The qty pricing tab content
    echo '<h2>Quantity Pricing</h2>';
    echo '<p>Here\'s your quantity pricing tab.</p>';
}
function woo_other_products_tab_content() {
    // The other products tab content
    echo '<h2>Other Products</h2>';
    echo '<p>Here\'s your other products tab.</p>';
}
?>

最佳答案

由于您使用了 4 次相同的 Hook woocommerce_product_tabs,您的问题来自第一个 Hook 的最高优先级。相反,您应该只使用它一次,将 4 个 Hook 函数合并为一个。

这是经过功能测试的代码,稍作改动:

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {

    // 1) Removing tabs

    unset( $tabs['description'] );              // Remove the description tab
    // unset( $tabs['reviews'] );               // Remove the reviews tab
    unset( $tabs['additional_information'] );   // Remove the additional information tab


    // 2 Adding new tabs and set the right order

    //Attribute Description tab
    $tabs['attrib_desc_tab'] = array(
        'title'     => __( 'Desc', 'woocommerce' ),
        'priority'  => 100,
        'callback'  => 'woo_attrib_desc_tab_content'
    );

    // Adds the qty pricing  tab
    $tabs['qty_pricing_tab'] = array(
        'title'     => __( 'Quantity Pricing', 'woocommerce' ),
        'priority'  => 110,
        'callback'  => 'woo_qty_pricing_tab_content'
    );

    // Adds the other products tab
    $tabs['other_products_tab'] = array(
        'title'     => __( 'Other Products', 'woocommerce' ),
        'priority'  => 120,
        'callback'  => 'woo_other_products_tab_content'
    );

    return $tabs;

}

// New Tab contents

function woo_attrib_desc_tab_content() {
    // The attribute description tab content
    echo '<h2>Description</h2>';
    echo '<p>Custom description tab.</p>';
}
function woo_qty_pricing_tab_content() {
    // The qty pricing tab content
    echo '<h2>Quantity Pricing</h2>';
    echo '<p>Here\'s your quantity pricing tab.</p>';
}
function woo_other_products_tab_content() {
    // The other products tab content
    echo '<h2>Other Products</h2>';
    echo '<p>Here\'s your other products tab.</p>';
}

此代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

测试和工作。


在同一个钩子(Hook)中,你可以:

  • 删除标签
  • 添加标签
  • 重新排序标签

相关官方文档:Editing product data tabs

关于php - 向 WooCommerce 单个产品页面添加多个选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964939/

相关文章:

php - 是否可以嵌套相同简码的 wordpress 简码?

mysql - 用于检索 WooCommerce 订单项和元数据的 SQL 选择查询

Android:用于在选项卡之间切换的滑动手势和动画

c++ - QTabBar图标位置

PHP:检查是否为空行 ("\n")

sql - mysql: 将所有表中的 http://old-domain.com 更改为 http://new-domain.com

php - 在 PHP 函数中使用数组作为默认参数

android - 无法为 "Galaxy Tab"制作全屏应用程序(Android 设备)

php - jQuery 和 onclick html 函数回调

php - 如何从加权列表中挑选 4 个独特的项目?