【问题标题】:WooCommerce how to hide "Description" tab if product description is empty如果产品描述为空,WooCommerce 如何隐藏“描述”选项卡
【发布时间】:2021-12-21 15:51:59
【问题描述】:

每当产品的长描述字段为空时,我想隐藏 WooCommerce 产品的“描述”标题。

我试过了:

// Remove Empty Tabs
add_filter( 'woocommerce_product_tabs', 'yikes_woo_remove_empty_tabs', 20, 1 );

function yikes_woo_remove_empty_tabs( $tabs ) {

if ( ! empty( $tabs ) ) {
    foreach ( $tabs as $title => $tab ) {
            if ( empty( $tab['content'] ) && strtolower( $tab['title'] ) !== 'description' ) {
                unset( $tabs[ $title ] );
            }
        }
    }
    return $tabs;
}

但这并没有最终隐藏标题。这个标题似乎不是一个实际的标签。

在渲染的源码中是这样的:

<div class="product-description">
    <h3 class="summary-description-title">Description</h3>
</div>

【问题讨论】:

  • 请附上您的html template,或说明您使用的是哪个主题,或者至少说明它在哪个页面上。是在single product 页面上还是在shop 页面上还是在archive 页面上?
  • 主题是 WP Rig。 HTML 模板只是单个产品视图的默认 WooCommerce。

标签: php wordpress woocommerce hook-woocommerce woocommerce-theming


【解决方案1】:

这是由 woocommerce 创建的“标签”。所以你可以检查它是否有内容,如果没有,那么你可以unset它。像这样:

add_filter('woocommerce_product_tabs', 'your_them_manipulating_description_tab', 99);

function your_them_manipulating_description_tab($tabs)
{
  global $product;

  $product_description = $product->get_description();

  if (!$product_description) 
  {
    unset($tabs['description']);
  }
  return $tabs;
}

它对我有用,如果你也可以让它工作,请告诉我!

【讨论】:

  • 谢谢。我在主题的functions.php 中实现了您的代码。但它仍然没有删除“描述”选项卡。我已经禁用了所有插件和相同的结果。当我切换到 Storefront 时,如果没有此代码,问题就会自行消失。我很难知道还有什么地方可以排除故障以找出原因。
  • 更新:我发现一个 WP Rig Component.php 文件控制了这个,而不是 WooCommerce 模板。我已经应用了添加到该 Component.php 文件中有意义的代码部分,并且问题已得到解决。谢谢!
猜你喜欢
  • 2020-05-12
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 2022-09-23
  • 2021-04-24
  • 2016-10-19
相关资源
最近更新 更多