2016-10-10 219 views
2

我想向WooCommerce添加三个自定义选项卡。我有下面的代码,其中两个显示,但由于某种原因,属性描述选项卡不显示在页面上。不仅数量定价标签不显示其描述。我试图将代码的不同部分移动到不同的位置,并且检查了代码中的错误或缺少的部分。这是尽可能接近我能得到它。将多个选项卡添加到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>'; 
} 
?> 
+0

“It broke the site”是95%的语法错误。你可以通过启用['WP_DEBUG'](https://codex.wordpress.org/Debugging_in_WordPress)来获得更有用的错误描述。 – helgatheviking

+0

我将Loic的代码添加到了我的functions.php的顶部,并得到:解析错误:语法错误,/ home/parkymay/public_html/demo/wp-content/themes/BuilderChild中的意外'add_filter'(T_STRING)第1行的functions.php – MattM

+0

错误说它在第1行,如果我不得不猜测,说明父主题的'functions.php'有问题。我用上面的整个'functions.php'作为Twenty Six的一个子主题,它不会产生任何错误。 – helgatheviking

回答

4

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

这里是你的功能测试的代码,改变了一点点:

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文件或也以任何插件文件。

经过测试和工作。


在同一个钩子,您可以:

  • 删除选项卡
  • 添加标签
  • 重新排列分页

相关的官方文件:Editing product data tabs

+1

经过测试,它对我很有用(不要以为你需要100个优先级,但它不会伤害任何东西),所以在你的'functions.php'中可能会出现语法错误。正如我上面的建议,你应该启用'WP_DEBUG'来找出在哪里。 – helgatheviking

0

唯一additi我做的是在添加它之前检查标签中是否存在内容。我简单地用'if'语句包装每个新数组。

if (!empty(get_the_content())) { 
    $tabs['tab'] = array(
     'title'  => __('Tab Title', 'woocommerce'), 
     'priority' => 100, 
     'callback' => 'woo_tab_content' 
    ); 
} 
相关问题