PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] Hide custom tab when no content is present in woocommerce products

 January 01, 2022     function, html, php, woocommerce, wordpress     No comments   

Issue

Is there a way I can hide the custom tabs if there is no content present in the field box. I am implementing this with advanced custom fields plugin. So far the tab is still present even if there is no content placed

Here is the code that I have placed in my functions.php

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

// Adds the new tab

    $tabs['direction_tab'] = array(
        'title'     => __( 'Direction', 'woocommerce' ),
        'priority'  => 60,
        'callback'  => 'woo_new_direction_tab_content'
    );

    return $tabs;

}


function woo_new_direction_tab_content() {

    // The new tab content

    echo the_field('directions');

}

UPDATE

//Direction Tab
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {

// Adds the new tab

    $tabs['direction_tab'] = array(
        'title'     => __( 'Direction', 'woocommerce' ),
        'priority'  => 60,
        'callback'  => 'woo_new_direction_tab_content'
    );


    return $tabs;





}


function woo_new_direction_tab_content() {

    if( get_field('directions') )
    {
        echo the_field('directions');
    }

    else
    {
        echo "<style>li.direction_tab_tab{ display:none !important; }</style>";
    }

}

Solution

There is most likely a better way of doing this, but I've achieved this in the past with the following:

if( get_field('directions') )
{
    echo the_field('directions');
}
else
{
    echo "<style>.direction_tab_tab { display:none !important; }</style>";
}

This will print the contents of the "directions" field if there is text in it, if not it will print the css and hide the tab.



Answered By - Howli
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing