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



我整天都在把头发拉开,请原谅简短的描述,我只需要验证我的理智!

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

无论我尝试以哪种方式覆盖单个product.php并使此文件使用逻辑检查product_cat并相应地给出模板,我要么"未加载页面"或"我写的内容"被跳过并默认已加载。

到目前为止,我已经多次使用以下方法,试图将可能过时的代码拼凑在一起或以其他方式引起所有大惊小怪:

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

WooCommerce单产品 - 类别模板

为某些产品类别创建一个不同的模板文件-WordPress/WooCommerce?

我更希望有人对此有所了解,我显然错过了一些文章,其中有很多关于尝试和大多数成功的文章,但我无法做到。

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

目前还没有成功,但这是我要;

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

  • /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(第39行(
  • 添加了其他ID
  • /mytheme/woocommerce/单产品团队/文件夹,所有单个产品文件都要更改。

正如我上面说的那样,这不起作用,但希望我提供的问题可能更为明显。

再次感谢您的任何帮助:(

[以为我已经知道]

好吧,所以我认为我有效的东西,至少现在似乎仍然需要进一步的测试,但是任何想法都非常欢迎,这就是我到目前为止的一种我的主题中的产品团队 - 商店文件夹

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;
}

使用"自定义"类别中任何产品的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;
}

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

最新更新