Woocommerce使用single-product.php重定向多个单一产品模板 [英] Woocommerce Multiple single product templates using single-product.php to redirect

查看:319
本文介绍了Woocommerce使用single-product.php重定向多个单一产品模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

无论我尝试以哪种方式覆盖single-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/single-product-team-shops/包含所有单个产品文件的文件夹.

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

再次感谢您的帮助:)

[认为我已经掌握了]

好,所以我认为我有一个可行的方法,至少目前看来,还需要做一些进一步的测试,但是任何想法都值得欢迎,这是我到目前为止所做的一项工作,我的主题中的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;
}

解决方案

对自定义"类别中的任何产品使用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相同的外观.您可以重命名"所有挂钩,然后可以将现有功能(例如用于添加到购物车按钮的功能)添加到新挂钩中,以实现完全自定义的外观.

I've been pulling my hair out with this all day, please forgive the short description, I just need to validate my sanity!!

As the title says, I'm trying to create two or three different single-product layouts within woocommerce. The minimum is trying to achieve would be to have multiple single-product folders each with their own name and configurations.

No matter which way I try to override the single-product.php and make this file use logic to check for the product_cat and give out templates accordingly, I either the page not loading or what I write is skipped over and the default is loaded.

So far I've been through the following methods multiple times, trying to piece together what may be outdated code or otherwise causing all the fuss:

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?

I was more hoping someone may know something about this that I'm obviously missing as there are many articles out there on what to try and most claim success but I'm unable to do so.

[Update] using template_include code from @helgatheviking

No success just yet but here's where I'm up to;

File structure team-shops is the category I'm trying to get

  • /mytheme/woocommerce/single-product.php - no changes
  • /mytheme/woocommerce/content-single-product.php
  • /mytheme/woocommerce/single-product-team-shops.php - changed line 37 to<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
  • /mytheme/woocommerce/content-single-product-team-shops.php - added additional id to #product-id (line 39)
  • /mytheme/woocommerce/single-product-team-shops/ folder with all single product files to change.

As I said above this isn't working but hopefully with what I've provided the problem may be more obvious.

Thanks again for any help :)

[Think I've got it]

Ok so I think I've something that works, at least for now it seems to, still have some further testing to do but any thoughts more than welcome, this is what I've got so far along with a single-product-team-shops folder in my theme

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

解决方案

Use a single-product-custom.php template for any product in the "custom" category:

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: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.

这篇关于Woocommerce使用single-product.php重定向多个单一产品模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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