2017-04-25 42 views
0

我一直在用这一整天拉我的头发,请原谅简短的描述,我只需要验证我的理智!Woocommerce使用single-product.php重定向的多个单一产品模板

正如标题所说,我试图在woocommerce中创建两个或三个不同的单一产品布局。最低限度试图达到的目标是拥有多个单独的产品文件夹,每个文件夹都有自己的名称和配置。

无论我试图重写单一product.php并使该文件使用逻辑来检查product_cat并相应地发出模板,我要么不加载页面,要么跳过我写的内容,默认加载。

到目前为止,我已经多次经历过下面的方法,试图拼凑出什么可能是过时的代码或以其他方式引起大惊小怪:

WooCommerce - How to create multiple single product template based on category?

Woocommerce single product - template by categories

Creating a different template file for certain Product Categories - Wordpress/Woocommerce?

我更希望有人可能知道这件事,我明显失踪了,因为那里有很多关于该怎么尝试和多数宣称成功的文章但我无法这样做。

[更新]使用template_include代码@helgatheviking

没有成功,只是还没有,但这里的地方我到;

文件结构 团队商店是我试图让

  • /mytheme/woocommerce/single-product.php类别 - 没有改变
  • /mytheme的/ woocommerce /内容单product.php
  • /mytheme/woocommerce/single-product-team-shops.php - 改变37行至<?php wc_get_template_part('content', 'single-product-team-shops'); ?>
  • /mytheme/woocommerce/content-single-product-team-shops.php - 加入另外的id来#product-id(line 39)
  • /mytheme/woocommerce/single-product-team-shops /文件夹与所有单个产品文件进行更改。

正如我上面所说,这是行不通的,但希望我提供的问题可能会更明显。再次

感谢所有帮助:)

[我想我知道了]

好了,所以我想我东西的作品,至少在现在看来,仍然有一些进一步测试可以做,但任何想法多人欢迎,这是我走到这一步,随着单一产品团队商店在我的主题

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

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

    $term_id = 2854; 
    $taxonomy_name = 'product_cat'; 
    $term_children = get_term_children($term_id, $taxonomy_name); 

    foreach ($term_children as $child) { 

    // on single posts with mock category and only for single-product/something.php templates 
    if(is_product() && has_term($child, '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-team-shops/", $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; 
} 
+0

我想说这听起来非常熟悉,但在我的第一个链接[这里](http://stackoverflow.com/a/25797963/383847)中有答案。我的'template_include'方法应该让你运行多个单一产品模板。它没有看起来过时(尽管我现在无法重新测试),所以请分享您的代码版本并解释您存储模板的位置。 – helgatheviking

+0

感谢@helgatheviking生病根据您的链接获取所有设置并回复您,感谢您的支持! –

+0

已更新@helgatheviking –

回答

2

使用在一个single-product-custom.php模板任何产品文件夹“定制“类别:

add_filter('template_include', 'so_43621049_template_include'); 

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

注意:如果您在single-product-custom.php模板中使用相同的动作钩子,您将获得与默认single-product.php相同的外观。你可以'重命名'所有的钩子,然后添加现有的功能(例如添加到购物车按钮等)到新的钩子,以实现完全自定义的外观。

+0

伟大的提示和似乎是做到这一点的最好和最明确的方式。只是混淆了你的意思是“重命名”钩子。我知道我可以做'add_action('woocommerce_before_single_product','mytheme_dostuff',10);'然后适用于所有模板。重命名如何/在哪里进来? – Trevor

+1

在'single-product-custom.php'中,你可以将'woocommerce_before_single_product'动作钩子切换到'do_action('my_theme_before_single_product')',然后你可以按照你喜欢的任何顺序向* that * new钩子添加动作。 – helgatheviking

+0

hey,@helgatheviking我遇到了一个与此代码相关的bug,即woocommerce选项卡中的评论选项卡完全错过了,但仅限于原始单产品模板,使用新创建的模板时,一切都显示正常档 –