【问题标题】:Sage 9 overwriting Woocommerce templatesSage 9 覆盖 Woocommerce 模板
【发布时间】:2018-03-27 12:45:31
【问题描述】:

我正在尝试使用新的 Wordpress Sage 覆盖 Woocommerce 模板文件。我的问题是带有刀片扩展的新 Sage 版本无法识别旧的 Woocommerce 模板文件。

我曾经将 Woocommerce 模板复制到我的主题文件夹以覆盖一直有效的模板,但自从新的 Sage 9 以来,这不再有效。

这是我正在使用的 Sage 版本:https://github.com/roots/sage (9)

这是我正在使用的 Woocommerce 版本:https://github.com/woocommerce/woocommerce (3.3.4)

【问题讨论】:

    标签: php html woocommerce blade roots-sage


    【解决方案1】:

    您可以查看https://github.com/mtx-z/Sage9-Woocommerce-Integration,最新的 Sage 9.0.1、Woocommerce 3.4.3(这是我的)。

    我用它来将 Sage 功能与 Woocommerce 兼容的主题一起使用。

    【讨论】:

      【解决方案2】:

      在您的 sage_theme/app/setup.php 文件中添加: add_theme_support('woocommerce'); 与你的土壤主题支持。

      在您的 sage_theme/resources/views 文件夹中创建一个 woocommerce.blade.php 文件。将以下代码添加到这个新文件中:

      @php if ( !defined( 'ABSPATH' ) ) { exit; } @endphp
      
      @extends( 'layouts.app' )
      
      @section( 'content' )
          @if ( is_product() ) {{-- if single product --}}
              @php
                  /**
                  * woocommerce_before_main_content hook.
                   *
                  * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
                  * @hooked woocommerce_breadcrumb - 20
                  */
                  do_action( 'woocommerce_before_main_content' );
              @endphp
      
              @while ( have_posts() ) @php the_post(); @endphp
      
                  @php wc_get_template_part( 'content', 'single-product' ); @endphp
      
              @endwhile
      
              @php
                  /**
                  * woocommerce_after_main_content hook.
                  *
                  * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
                  */
                  do_action( 'woocommerce_after_main_content' );
              @endphp
          @else {{-- if product archive (any) --}}
              @if ( woocommerce_product_loop() ) 
      
                  @php
                      /**
                      * Hook: woocommerce_before_shop_loop.
                      *
                      * @hooked woocommerce_output_all_notices - 10
                      * @hooked woocommerce_result_count - 20
                      * @hooked woocommerce_catalog_ordering - 30
                      */
                      do_action( 'woocommerce_before_shop_loop' );
      
                      woocommerce_product_loop_start();
                  @endphp
      
                  @if ( wc_get_loop_prop( 'total' ) ) 
                      @while ( have_posts() ) @php the_post(); @endphp
      
                          @php
                              /**
                              * Hook: woocommerce_shop_loop.
                              *
                              * @hooked WC_Structured_Data::generate_product_data() - 10
                              */
                              do_action( 'woocommerce_shop_loop' );
      
                              wc_get_template_part( 'content', 'product' );
                          @endphp
                      @endwhile
                  @endif
      
                  @php
                      woocommerce_product_loop_end();
      
                      /**
                      * Hook: woocommerce_after_shop_loop.
                      *
                      * @hooked woocommerce_pagination - 10
                      */
                      do_action( 'woocommerce_after_shop_loop' );
                  @endphp
              @else
                  @php
                      /**
                      * Hook: woocommerce_no_products_found.
                      *
                      * @hooked wc_no_products_found - 10
                      */
                      do_action( 'woocommerce_no_products_found' );
                  @endphp
              @endif
          @endif
      @endsection
      

      在您的 sage_theme/resources/ 文件夹中创建一个名为 woocommerce 的新文件夹。在该文件夹中,您需要两个文件,archive-product.phpsingle-product.php。这两个文件都应该包含:<?php echo App\Template('woocommerce'); ?>

      现在你可以开始了。

      所有 Woo 流量都将通过 sage_theme/resources/views/woocommerce.blade.php 页面进行过滤,然后将它们推送到 sage_theme/resources/woocommerce 文件夹您可以像往常一样覆盖 Woo 模板文件。

      如果您需要编辑通常在 archive-product.phpsingle-product.php 中的内容,您将在 sage_theme 中进行/resources/views/woocommerce.blade.php 文件。

      可能有一种方法,在 sage_theme/resources/woocommerce/archive-product.phpsage_theme/resources/woocommerce/single-product.php 中链接到单独的文件而不是相同的 woocommerce.blade.php 文件,但我还没有时间尝试。

      【讨论】:

        【解决方案3】:

        首先,尝试将其放入resources/woocommerce

        如果这不起作用,您可能需要手动将模板路径注册到Blade

        将以下代码添加到 app/filters.php 并更新它以相应地引用您的 WooCommerce 路径 (resources/views/woocommerce/{your-template}.php)。

        @mmirus 在the Roots forums 中对我在此处发布的类似问题表示赞赏。

        免责声明:我尚未对此进行测试,本周将进行测试。但是用 Blade 注册它的理论是正确的。

        有关该主题的有用链接:

        【讨论】:

          猜你喜欢
          • 2020-08-03
          • 2013-02-26
          • 2013-02-08
          • 1970-01-01
          • 1970-01-01
          • 2018-02-01
          • 2015-11-16
          • 2023-03-21
          • 2016-12-28
          相关资源
          最近更新 更多