Woocommerce使用single-product.php重定向多个单一产品模板

bla*_*l24 1 php wordpress logic woocommerce

我整天都在梳头,请原谅简短说明,我只需要验证我的理智即可!!

如标题所示,我正在尝试在woocommerce中创建两个或三个不同的单产品布局。试图实现的最低要求是拥有多个单一产品文件夹,每个文件夹都有自己的名称和配置。

无论我尝试以哪种方式覆盖single-product.php并使该文件使用逻辑来检查product_cat并相应地给出模板,我要么页面未加载,要么跳过我写的内容,并加载了默认值。

到目前为止,我已经多次尝试以下方法,试图将可能过时的代码拼凑在一起,否则会引起大惊小怪:

WooCommerce-如何基于类别创建多个单一产品模板?

Woocommerce单一产品-按类别的模板

为某些产品类别创建其他模板文件-Wordpress / Woocommerce?

我更希望有人可能对此有所了解,但我显然很想不到,因为有很多文章尝试了哪些方法,并且大多数方法都声称成功,但是我却做不到。

[更新]使用template_include@helgatheviking中的代码

尚无成功,但这是我要做的。

文件结构团队商店是我想要获得的类别

  • /mytheme/woocommerce/single-product.php-无更改
  • /mytheme/woocommerce/content-single-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-在#product-id中添加了其他ID(第39行)
  • / mytheme / woocommerce / single-product-team-shops /文件夹,并更改所有单个产品文件。

就像我在上面说的那样,这是行不通的,但是希望我提供的问题可能更加明显。

再次感谢任何帮助 :)

[想我知道了]

好的,所以我认为我有一个可行的方法,至少到目前为止,似乎还需要做进一步的测试,但是任何想法都值得欢迎,这是到目前为止我与一个单一产品团队一起所取得的成就我的主题中的-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;
}
Run Code Online (Sandbox Code Playgroud)

hel*_*ing 5

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;
}
Run Code Online (Sandbox Code Playgroud)

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