Custom Layout for Single Product page - Woocommerce

浪尽此生 提交于 2021-02-10 20:49:37

问题


I am trying to create a custom layout for one of our product pages in Woocommerce. I have searched and followed so many tutorials, but none are working for me. I copied 'single-product.php' and placed it into my active child theme in the folder 'woocommerce'. I then duplicated this and renamed it 'single-product-landing.php'.

I then placed the following code into my functions.php page:

add_filter( 'template_include',     'custom_single_product_template_include', 10 );

function custom_single_product_template_include( $template ) {
if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-landing.php';
 } 
return $template;
}

Then I tried changing items in single-product-landing.php but nothing is changing, so it can't be picking it up. To confirm, I have the product assigned to the 'custom' category.

I should also add that my product is a 'variable' product, am not sure if that has any affect on anything here.

EDIT: I have found the following code in my 'single.php' file - I think this may be interfering. Problem is, if I delete this nothing shows on any of my pages (even those not affected by the new template):

 <?php
      if(get_theme_mod('product_layout') == 'custom') {
        wc_get_template_part( 'content', 'single-product-custom' );
      } else {
        wc_get_template_part( 'content', 'single-product' );
      }
  ?>

Any idea how I can modify this to still have content show but not over-rule the template change that i'm trying to make?


回答1:


  1. Page templates are loaded after the template_filter has been run, so adding that code to a page template file will have no effect. To make that run, place the same code in your child theme's functions.php.

  2. The 566 at the end of the add_filter line refers to the filter's loading priority and not a category id. Simply put, the higher the number the later the filter will be loaded.

  3. Finally, your if statement can be simplified a little using Woocommerce functions - if ( is_product() && ( has_term( 'custom', 'product_cat') ) ) { - doesn't make much difference, but it's tidier.



来源:https://stackoverflow.com/questions/52103732/custom-layout-for-single-product-page-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!