在Woocommerce中将自定义单一产品模板用于特定产品类别 [英] Using a custom single product template for a specific product category in Woocommerce

查看:259
本文介绍了在Woocommerce中将自定义单一产品模板用于特定产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对woocommerce商店中的一种产品使用自定义页面。我一直在尝试应用附加功能,但没有成功(该代码来自
具有活动主题:


  • 如果未启用,则应搜索并查看相关的Woocommerce文档。


  • 其中一个应解决您的问题。


    I'm trying to use a custom page for only one of the products in the woocommerce shop. I've been trying to apply the attached function but without success (which code is coming from this answer).

    I have a created a copy of the single-product.php file in my /woocommerce folder with some added code, but the single product view only shows the "standard" single-product-php and not my single-product-mock.php file.

    And the product does have the product category "custom".

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

    解决方案

    The code that you are using works just perfectly. Your code is a bit incomplete too:

    add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
    function custom_single_product_template_include( $template ) {
        if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
            $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';
        } 
        return $template;
    }
    

    So the problem could be related to:

    1. The location of that custom template, that should be in a 'woocommerce' folder inside your active child theme (or inside your active theme).
    2. Woocommerce support need to be enable for your theme.

    Solutions:

    1. be sure that inside your active child theme (or active theme) there is a "woocommerce" folder and add inside it your custom template single-product-mock.php
      (but not inside a "templates" sub-folder)
    2. To check Woocommerce support is enabled:
      • Copy the default single-product.php template inside the "woocommerce" folder located in your active child theme (or active theme)
      • Go in backend Woocommerce > Status … and you will normally see in "templates" section (at the end of this page):

        With an active child-theme:
        With an active theme:
      • If is not enable, you should search and look to the related Woocommerce documentation.

    One of those, should solve your issue.

    这篇关于在Woocommerce中将自定义单一产品模板用于特定产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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