WooCommerce - 如何根据类别创建多个单一产品模板? [英] WooCommerce - How to create multiple single product template based on category?

查看:26
本文介绍了WooCommerce - 如何根据类别创建多个单一产品模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是 woocommerce 的新手,我的商店有四类产品,它们使用相同的单一产品模板.我想添加第五类产品,其中产品页面布局与已经使用的产品非常不同.

Hello I am fairly new to woocommerce, My shop has four categories of products which use same single-product template. I want to add a fifth category of products where the product page layout is very different from already the one used.

这是文件结构 -

  • 主题/woocommerce/单一产品/
  • theme/woocommerce/single-product-mock/
  • theme/woocommerce/single-product-mock/title.php
  • theme/woocommerce/content-single-product-mock.php
  • theme/woocommerce/single-product.php

我创建了一个名为 content-single-product-mock.php 的文件.在 single-product.php 中使用以下代码

I created a file called content-single-product-mock.php. In single-product.php using the following code

        <?php if (has_term( 'mock', 'product_cat' )) {
            woocommerce_get_template_part( 'content', 'single-product-mock' );
        } else{
         wc_get_template_part( 'content', 'single-product' ); 
        } ?>

对于类别模拟,它重定向到 content-single-product-mock.php,但它使用 single-product 文件夹中的模板文件.如何更改content-single-product-mock.php中的模板路径,使其使用single-product-mock文件夹中的自定义文件?

For the category mock it redirects to content-single-product-mock.php, but it uses the template files in single-product folder. How do change the template path in content-single-product-mock.php so that it uses the customized files in single-product-mock folder?

推荐答案

可能有不止一种方法可以做到这一点,但它们都扭转了在模板被包含之前过滤模板的想法.

There is probably more than one way to do this, but they all kind of turn around the idea of filtering the template before it is included.

可以完全跳过 WooCommerce 的 simple-product.php 模板(无需覆盖该模板)并直接转到 simple-product-mock.php 并在那里创建所有内容.您可以通过过滤 template_include 来做到这一点.>

You could totally skip WooCommerce's simple-product.php template (without needing to override that template) and go directly to simple-product-mock.php and create everything there. You'd do that by filtering template_include.

add_filter( 'template_include', 'so_25789472_template_include' );

function so_25789472_template_include( $template ) {
  if ( is_singular('product') && (has_term( 'mock', 'product_cat')) ) {
    $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';
  } 
  return $template;
}

您可以编辑single-product-mock.php 来调用content-single-product-mock.php 并对其进行硬编码文件.没有什么需要你继续使用 Woo 的钩子和函数.它们的目的只是让您轻松自定义.

You could edit single-product-mock.php to call content-single-product-mock.php and hard-code that file. Nothing requires you to keep using Woo's hooks and functions. The point of them is just to make it easy for you to customize.

或者更棘手的是,您可以将诸如 single-product/title.php 之类的模板复制到 single-product-mock 文件夹中...例如:single-product-mock/title.php 然后任何时候我们在 mock 类别中的单个产品模板上,我们都会拦截对 single-product/something.php<的调用/code> 模板并将它们重定向到 single-product-mock/something.php if 它存在并继续指向 single-product/something.php 如果没有.我们将通过 woocommerce_locate_template 过滤器执行此操作.

Or to be really tricky, you could duplicate templates such as single-product/title.php into a single-product-mock folder... ex: single-product-mock/title.php and then any time we're on a single product template in the mock category, we'll intercept calls to the single-product/something.php template and redirect them to single-product-mock/something.php if it exists and keep pointing to the single-product/something.php if it does not. We'll do this via the woocommerce_locate_template filter.

add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );

function so_25789472_locate_template( $template, $template_name, $template_path ){

    // on single posts with mock category and only for single-product/something.php templates
    if( is_product() && has_term( 'mock', 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){

        // replace single-product with single-product-mock in template name
        $mock_template_name = str_replace("single-product/", "single-product-mock/", $template_name );

        // look for templates in the single-product-mock/ folder
        $mock_template = locate_template(
            array(
                trailingslashit( $template_path ) . $mock_template_name,
                $mock_template_name
            )
        );

        // if found, replace template with that in the single-product-mock/ folder
        if ( $mock_template ) {
            $template = $mock_template;
        }
    }

    return $template;
}

更新以过滤 wc_get_template.

/**
 * Change wc template part for product with a specific category
 *
 * @param  string $templates
 * @param  string $slug
 * @param  string $name
 * @return string
 */
function so_25789472_get_template_part( $template, $slug, $name ) {
  if ( $slug == 'content' && $name = 'single-product' && has_term( 'test', 'product_cat' ) ) {
    $template = locate_template( array( WC()->template_path() . 'content-single-product-test.php' ) );
  } 
  return $template;
}
add_filter( 'wc_get_template_part', 'so_25789472_get_template_part', 10, 3 );

这篇关于WooCommerce - 如何根据类别创建多个单一产品模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆