【问题标题】:How to hide woocommerce custom tab when the content is empty内容为空时如何隐藏woocommerce自定义选项卡
【发布时间】:2020-02-18 23:39:32
【问题描述】:

我只是在 woocommerce 单品页面中添加了 4 个自定义标签。现在,如果没有内容,我想隐藏这些选项卡。这是我的自定义标签代码。我怎样才能做到这一点?如果没有内容,则不显示自动描述选项卡。另一个问题是关于评论标签的。如果没有评论或 cmets,我也可以隐藏它吗? 提前致谢。

// Add a custom metabox

add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
    'add_product_metabox_additional_tabs',
    __( 'Additional product Tabs', 'woocommerce' ),
    'additional_product_tabs_metabox_content',
    'product',
    'normal',
    'high'
);
}

//  Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Indicated For
echo '<h4>' . __( 'Indicated for', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_indicated_for', true );
wp_editor( $value, '_indicated_for', array( 'editor_height' => 100 ) );

// Caution
echo '<br><hr><h4>' . __( 'Caution', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_caution', true );
wp_editor( $value, '_caution', array( 'editor_height' => 100 ) );


// How To
echo '<br><hr><h4>' . __( 'How To', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_how_to', true );
wp_editor( $value, '_how_to', array( 'editor_height' => 100 ) );

//Part of Cure Series

echo '<br><hr><h4>' . __( 'Part of Cure Series', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_part_of_cure_series', true );
wp_editor( $value, '_part_of_cure_series', array( 'editor_height' => 100 ) );

// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}


// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {

// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
    return $post_id;
}

//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
    return $post_id;
}

// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return $post_id;
}

if ( ! current_user_can( 'edit_product', $post_id ) ) {
    return $post_id;
}

// Sanitize user input and save the post meta fields values.

if( isset($_POST[ '_indicated_for' ]) )
    update_post_meta( $post_id, '_indicated_for', wp_kses_post($_POST[ '_indicated_for' ]) );

if( isset($_POST[ '_caution' ]) )
    update_post_meta( $post_id, '_caution', wp_kses_post($_POST[ '_caution' ]) );

if( isset($_POST[ '_how_to' ]) )
    update_post_meta( $post_id, '_how_to', wp_kses_post($_POST[ '_how_to' ]) );

if( isset($_POST[ '_part_of_cure_series' ]) )
    update_post_meta( $post_id, '_part_of_cure_series', wp_kses_post($_POST[ '_part_of_cure_series' ]) );

}

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['additional_information'] );   // Remove the additional information tab



//Attribute Indicated for
$tabs['intend_tab'] = array(
    'title'     => __( 'Intend For', 'woocommerce' ),
    'priority'  => 50,
    'callback'  => 'woo_indicated_for_tab_content'
);

// Adds the Caution tab
$tabs['caution_tab'] = array(
    'title'     => __( 'Caution', 'woocommerce' ),
    'priority'  => 60,
    'callback'  => 'woo_caution_tab_content'
);

// Adds the how to tab
$tabs['how_to'] = array(
    'title'     => __( 'How To', 'woocommerce' ),
    'priority'  => 70,
    'callback'  => 'woo_how_to_content'
);

 // Adds the _part_of_cure_series

$tabs['part_of_cure_series'] = array(
    'title'     => __( 'Part of Cure Series', 'woocommerce' ),
    'priority'  => 80,
    'callback'  => 'woo_part_of_cure_series'
);



$tabs['reviews']['priority'] = 90;

return $tabs;
}


function woo_indicated_for_tab_content()  {
global $product;


echo'<div><p>'. $product->get_meta( '_indicated_for' ) . '</p></div>';
}

function woo_caution_tab_content()  {
global $product;

echo'<div><p>'. $product->get_meta( '_caution' ) . '</p></div>';
}

function woo_how_to_content()  {
global $product;

echo'<div><p>'. $product->get_meta( '_how_to' ) . '</p></div>';
}

function woo_part_of_cure_series()  {
global $product;

echo'<div><p>'. $product->get_meta( '_part_of_cure_series' ) . '</p></div>';
}

【问题讨论】:

    标签: php wordpress woocommerce e-commerce


    【解决方案1】:

    我知道这是很晚的回答,但这是通用的,可能对某人有用

        /**
         * Remove product data tabs when the content is empty
         */
        function demo_remove_product_tabs( $tabs ) {
            foreach ( $tabs as $key=>$product_tab) {
                if ( isset( $product_tab['callback'] ) ) {
                    ob_start();
                    call_user_func( $product_tab['callback'], $key, $product_tab );
                    $tab_content = ob_get_clean();
                    if (empty($tab_content)){
                        unset($tabs[$key]); 
                    }
                }
            }
            return $tabs;
        }
        add_filter( 'woocommerce_product_tabs', 'demo_remove_product_tabs', 98 );
    

    【讨论】:

      【解决方案2】:

      在您的活动主题的functions.php中添加以下代码sn-ps来实现上述 -

      function modify_woo_custom_product_tabs( $tabs ) {
          global $product;
          // Remove tabs if contents data are empty
          if( isset($tabs['intend_tab'] ) && !$product->get_meta( '_indicated_for' ) ) {
              unset( $tabs['intend_tab'] ); 
          }
          if( isset($tabs['caution_tab'] ) && !$product->get_meta( '_caution' ) ) {
              unset( $tabs['caution_tab'] ); 
          }
          if( isset($tabs['how_to'] ) && !$product->get_meta( '_how_to' ) ) {
              unset( $tabs['how_to'] ); 
          }
          if( isset($tabs['part_of_cure_series'] ) && !$product->get_meta( '_part_of_cure_series' ) ) {
              unset( $tabs['part_of_cure_series'] ); 
          }
          if( isset($tabs['reviews'] ) && $product->get_review_count() ) {
              unset( $tabs['reviews'] ); // unset reviews
          }
          return $tabs;
      
      }
      add_filter( 'woocommerce_product_tabs', 'modify_woo_custom_product_tabs', 999 );
      

      【讨论】:

        猜你喜欢
        • 2021-07-12
        • 2015-01-09
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多