1

Forgive me – my Wordpress/PHP skills are hilariously rudimentary, so I may not use the correct terminology.

I have added a custom tab to the WooCommerce single product page and need it to display a number (sometimes single, sometimes multiple) of term meta values related to a custom taxonomy, 'book-author'. These values are created in a WYSIWYG editor and contain HTML formatting. Each 'book-author' has an author bio (the term meta 'wpcf-author-biography'), and each WooCommerce product has one or more 'book-authors' attributed to it.

This is the code I'm currently using:

// Add author bio tab

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

// Adds the new tab

$tabs['author_tab'] = array(
    'title'     => __( 'About the author', 'woocommerce' ),
    'priority'  => 50,
    'callback'  => 'woo_new_product_tab_content'
);

return $tabs;

}

// Add the content

function woo_new_product_tab_content() {

$terms = get_the_terms(get_the_ID(), 'book-author' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
    $all_term_meta = get_term_meta($term->term_id, 'wpcf-author-biography', false);
    print_r($all_term_meta);
}
}   
}

It took me forever to get to this point, and while the relevant data is finally being output, the frontend result leaves a lot to be desired. This is how it ends up:

<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel 
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">
            Array
(
[0] =&gt; XX staff writer&nbsp;<strong>John Doe</strong>&nbsp;has covered 
everything from men’s&nbsp;style and grooming to food, drinks and travel. John 
has also interviewed&nbsp;some of the biggest names in film, television and 
music.
)
Array
(
[0] =&gt; <div class="woocommerce-tabs wc-tabs-wrapper">
<div id="tab-test_tab" class="woocommerce-Tabs-panel woocommerce-Tabs-panel--
test_tab panel entry-content wc-tab" style="display: none;">

<strong>Jane Doe&nbsp;</strong>is an author and university 
lecturer&nbsp;in underwater basket-weaving. She has recently completed a PhD.

</div>
</div>
)
        </div>

While the tab itself is created without any issues, as you can see, I'm getting some rogue Arrays being displayed as well as some other guff, and the second term meta value is for some reason being stuck in its own hidden tab wrapper, when it should appear in the same tab.

Obviously I'm not going about this the right way otherwise I wouldn't be here, so any help or push in the right direction would be greatly appreciated.

Additionally, if there's any way to implement a condition so that if there is no 'book-author' attributed to the product, no 'About the author' tab appears, that would be most helpful...

Thanks in advance!

Edit: Based on the advice given by WebElaine I tried using a foreach instead of print_r:

function woo_new_product_tab_content() {

$terms = get_the_terms(get_the_ID(), 'book-author' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
    $all_term_meta = get_term_meta($term->term_id, 'wpcf-author-biography', 
false);     
    foreach($all_term_meta as $meta) {
        echo $meta[0]; //tried with return as well with no success
}
}
}
}

This yields the following results:

<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel 
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">
            G&lt;           </div>

If I remove the [0] after $meta, we manage to get rid of the ugly characters and stray Array, but I still have the issue of my second author's info getting lost:

<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel 
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">

        XX staff writer&nbsp;<strong>John Doe</strong>&nbsp;has covered 
everything from men’s&nbsp;style and grooming to food, drinks and travel. John 
has also interviewed&nbsp;some of the biggest names in film, television and 
music.

<div class="woocommerce-tabs wc-tabs-wrapper">
<div id="tab-test_tab" class="woocommerce-Tabs-panel woocommerce-Tabs-panel--
test_tab panel entry-content wc-tab" style="display: none;">

<strong>Jane Doe&nbsp;</strong>is an author and university lecturer&nbsp;in 
underwater basket-weaving. She has recently completed a PhD.

</div>
</div>          </div> 
0

For your author content display, instead of print_r($all_term_meta); try a foreach:

foreach($all_term_meta as $meta) {
    return $meta[0];
}

It's possible you may need to use "echo" instead of "return" but try "return" first.

To control whether or not there is an author tab, update your woo_new_product_tab function with the same condition:

function woo_new_product_tab( $tabs ) {
    $terms = get_the_terms(get_the_ID(), 'book-author' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        // Adds the new tab
        $tabs['author_tab'] = array(
            'title'     => __( 'About the author', 'woocommerce' ),
            'priority'  => 50,
            'callback'  => 'woo_new_product_tab_content'
        );
    }
    return $tabs;
}
  • Thanks for your answer! I tried the foreach with both return and echo, but no luck. Avoiding the complications of making the tab conditional at this stage, using return doesn't output anything on the front end (the tab is created but the content is empty), and echo has a different result (the tab is created and the content consists of >G. I'm probably doing something wrong – I've added what I'm doing in an edit. – ninox boobook May 18 '17 at 0:29
  • Getting rid of the [0] after $meta has brought some measure of success, but I'm still getting my second author hidden in a second invisible tab (see edit). – ninox boobook May 18 '17 at 0:50
0

Thanks to WebElaine I managed to find a solution that addressed the hidden tab issue:

function woo_new_product_tab_content() {

$terms = get_the_terms(get_the_ID(), 'book-author' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        $all_term_meta = get_term_meta($term->term_id, 'wpcf-author-
biography', false);     
        foreach($all_term_meta as $meta) {
            echo '<p>' . strip_tags($meta, '<strong><p><br>') . '</p>';
    }
   }
  }
}

This outputs the following satisfying HTML:

<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel 
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">

   <p>XX staff writer&nbsp;<strong>John Doe</strong>&nbsp;has 
   covered everything from men’s&nbsp;style and grooming to food, drinks and 
   travel. John has also interviewed&nbsp;some of the biggest names in film, 
   television and music.</p>

   <p><strong>Jane Doe&nbsp;</strong>is an author and university 
   lecturer&nbsp;in underwater basket-weaving. She has recently completed a 
   PhD.</p>         

</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.