donglu8334 2018-03-11 16:03
浏览 275
已采纳

在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
  • 写回答

1条回答 默认 最新

  • dtol41388 2018-03-11 18:30
    关注

    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:
        enter image description here With an active theme:
        enter image description here
      • If is not enable, you should search and look to the related Woocommerce documentation.

    One of those, should solve your issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU