【问题标题】:Move product meta to the description tab in WooCommerce将产品元数据移动到 WooCommerce 中的描述选项卡
【发布时间】:2020-01-07 10:51:00
【问题描述】:

如何将产品元数据移动到产品描述选项卡的开头? 我试试:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
add_action( 'woocommerce_product_tabs_description', 'woocommerce_template_single_meta', 10 );

删除有效,但 add_action() 无效。

【问题讨论】:

    标签: php wordpress templates woocommerce hook-woocommerce


    【解决方案1】:

    您可以保留第一行代码。然后在产品描述选项卡上插入单个产品元,在描述之前,您可以使用两种不同的方式:

    1)。使用 Hooks 如下:

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
    
    add_filter( 'woocommerce_product_tabs', 'woocommerce_custom_product_tabs', 999 );
    function woocommerce_custom_product_tabs( $tabs ) {
        // We overwrite the callback function with a custom one
        $tabs['description']['callback'] = 'woocommerce_product_meta_and_description_tab';
    
        // (optional) We can also overwrite the title
        $tabs['description']['title'] = __('Meta and description', 'woocommerce');
    
        return $tabs;
    }
    
    function woocommerce_product_meta_and_description_tab() { // this is where you indicate what appears in the description tab
        wc_get_template( 'single-product/meta.php' ); // The meta content first
        wc_get_template( 'single-product/tabs/description.php' ); // The product description after
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    2)。或覆盖模板:

    您可以按照this official documentation 中的说明通过您的主题覆盖single-product/tabs/description.php 模板文件。

    将文件复制到活动主题内的woocommerce 文件夹后,打开编辑single-product/tabs/description.php 文件并在其中添加以下行:

    wc_get_template( 'single-product/meta.php' );
    

    它将在产品描述选项卡中显示产品元信息。

    不要忘记保存在您的活动子主题的functions.php文件中:

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
    

    相关:WooCommerce action hooks and overriding templates

    【讨论】:

      【解决方案2】:

      实际上有一种方法可以在您的函数中执行此操作,而无需从 woocommerce 复制文件并在您的子主题中覆盖它们。

      根据 WooCommerce 文档,您可以自定义产品数据选项卡。对他们提供的代码稍作修改,您就可以按照您的要求进行操作:

      /**
       * Customize product data tabs
       */
      add_filter( 'woocommerce_product_tabs', 'woo_custom_description_tab', 98 );
      function woo_custom_description_tab( $tabs ) {
      
          $tabs['description']['callback'] = 'woo_custom_description_tab_content';    // Custom description callback
      
          return $tabs;
      }
      
      function woo_custom_description_tab_content() { // this is where you indicate what appears in the description tab
          wc_get_template( 'single-product/meta.php' ); // add this to add meta to content
          the_content(); // keep this in to preserve original functionality
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多