【问题标题】:How to remove woocommerce tab?如何删除 woocommerce 标签?
【发布时间】:2015-06-12 20:48:17
【问题描述】:

我们的 woocommerce 商店中的产品不需要任何默认选项卡,因此我设法禁用它们,因为我只需要在产品下方有产品描述,虽然我想保留实际描述,但我相信选项卡本身是多余的,因为没有任何其他选项卡。

基本上,我想完全删除选项卡的 & 标题,但保留其下方的内容框,而不修改 woocommerce 核心 php 模板文件。有没有办法在我的 WordPress 主题的 functions.php 中添加过滤器?

function woocommerce_default_product_tabs( $tabs = array() ) {
    global $product, $post;

    // Description tab - shows product content
    if ( $post->post_content ) {
        $tabs['description'] = array(
            'title'    => __( 'Description', 'woocommerce' ),
            'priority' => 10,
            'callback' => 'woocommerce_product_description_tab'
        );
    }

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming


    【解决方案1】:

    虽然 CSS 很棒,但如果样式表无法正确加载,您最终可能会毫无意义地向某人显示标签。正如您所提到的,最好在加载(服务器端)之前使用过滤器删除内容。

    请参阅 Woothemes 提供的以下代码以取消设置数据选项卡。
    编辑放置在主题内的 functions.php 文件中。

    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;
    }
    

    编辑 正如@BasvanDijk 所说 要完全删除,您可以使用以下方法

    add_filter( 'woocommerce_product_tabs', '__return_empty_array', 98 );
    

    【讨论】:

    • 只是出于好奇,您为什么将其优先级设为 98?
    • 除了确保它最后运行之外,可能是 99 的拼写错误?试图引导比我小 3 岁的我。
    • @logos_164 通常我使用 PHP_INT_MAX 作为我的优先级,以确保它在所有其他添加标签的插件之后运行。这样,您对产品页面上的内容拥有最终决定权!
    • 你最好只返回一个空数组。 function() { return []; } 或使用像 __return_empty_array() 这样的回调
    • @BasvanDijk - 很公平,但我想它会变成一维答案。我会补充你的建议
    【解决方案2】:
    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;
    }
    
    function woocommerce_template_product_description() {
        woocommerce_get_template( 'single-product/tabs/description.php' );
    }
    
    add_action( 'woocommerce_after_single_product_summary',  'woocommerce_template_product_description', 40 );
    

    我在这里所做的贡献为我工作。除了删除标签,还可以放回文本。

    感谢 Swapnali 和 Mustafa

    【讨论】:

      【解决方案3】:

      如果您想从 woo-commerce 产品详细信息页面中删除标签,请将此代码添加到您的 function.php

      选项 1-

      转到functions.php 并添加以下代码。 (转到管理面板 > 外观 > 编辑器 > functions.php)

      add_filter( 'woocommerce_product_tabs', 'woo_remove_tabs', 98 );
      function woo_remove_tabs( $tabs ){
          if(is_product()){
            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;
       }
      

      通过使用此过滤器,我们可以从 Woocommerce 产品页面中删除标签。

      选项 2-

      或者对于另一种方法,只需将其添加到您的 functions.php

      remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10);
      

      选项 3-

      通过将其添加到 woocommerce.css 底部来隐藏选项卡

       .woocommerce_tabs .tabs {
          display: none;
      }
      

      阅读更多 -Woo-commerce: Remove tab from product page

      【讨论】:

        【解决方案4】:

        对不起,问题不仅在于删除标签,还在于保留产品描述。如果您曾经尝试过上面的代码,您会意识到在删除选项卡时实际上是在删除产品描述。这不是我们想要的情况。

        您应该在某处添加以下代码以将其添加回来。但不幸的是,这次您可以在图片旁边添加描述并制作一个窄列。我找不到将它很好地添加到标签之前存在的图片下方的解决方案。 代码:

        function woocommerce_template_product_description() {
            woocommerce_get_template( 'single-product/tabs/description.php' );
        }
        
        add_action( 'woocommerce_single_product_summary',  'woocommerce_template_product_description', 40 );
        

        【讨论】:

        • 难道你不能只使用我提供的内容的变体,因为你没有取消设置描述吗? ie - 注释掉 unset($tabs['description']);
        【解决方案5】:

        这是工作代码:

        add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_reviews_tab', 98 );
        function wcs_woo_remove_reviews_tab($tabs) {
        unset($tabs['reviews']);
        return $tabs;
        }
        

        【讨论】:

          【解决方案6】:

          由于某种原因,添加到functions.php文件的代码对我不起作用,即使它在woo commerce codex中。

          我收到了大量针对这些显示评论的产品的垃圾评论。

          最后,我使用内置的 wordpress/woocommerce 功能从所有产品中手动删除了评论标签。

          1. 转到产品列表页面
          2. 单击复选框以选择所有产品(它只会选择此页面上的产品,因此您可能需要浏览几页才能重复)
          3. 从批量操作下拉菜单中,选择编辑
          4. 点击应用
          5. 您现在将看到您可以执行的所有批量编辑操作。选择“cmets”下拉菜单,然后选择“不允许”
          6. 点击更新
          7. 如果使用缓存插件,请确保删除所有缓存

          【讨论】:

            【解决方案7】:

            结合上述答案,将函数嵌套在动作中,以便您可以在条件中使用:

            add_action( 'wp', 'custom_remove_tabs_by_tag' );
            function custom_remove_tabs_by_tag() {
            if ( is_product() && has_term( 'tag-term', 'product_tag' ) ) 
            {    
                // Remove All Product Tabs
                remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
                // Add Product Description
                add_action( 'woocommerce_after_single_product_summary',
                             function () { woocommerce_get_template( 'single-product/tabs/description.php' );}, 
                             40 );
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-10-16
              • 2018-08-12
              • 1970-01-01
              • 2016-11-19
              • 2013-01-27
              • 1970-01-01
              • 1970-01-01
              • 2015-01-23
              相关资源
              最近更新 更多