2

Sorry in advance for my approximative english. I would like to change product page style on woocommerce for a specific product category (don't display pictures and some other important layout changes). Already made it and it works, the problem is that I would like to display these products on a single page. I tried using woocommerce integrated shortcode [product_page id=$id], the problem is that the custom style and layout I created on content-single-product.php (in woocommerce folder) is not applied using this shortcode. I tried to copy my script on class-ws-shortcodes.php (woocommerce folder) but it didn't worked. What should I do to display the products on a specific page, with the real style of these products page (the custom one I created), and not the woocommerce shortcode one?

Thanks in advance for your answers

Ben
  • 23
  • 1
  • 4
  • See https://stackoverflow.com/help/how-to-ask. – ljk321 May 01 '15 at 09:09
  • check URL : https://wordpress.org/support/topic/product-page-11 It might help you – Ashish Patel May 01 '15 at 09:27
  • Thanks for your answer, I tried your solution replacing term_id by product category id, and "single-product-seperateCategory" by "single-product-custom" (name of my category and of the php file copied in theme folder) but it didn't worked for me – Ben May 01 '15 at 10:24

3 Answers3

4

There are two ways to achieve this, it just depends on whether you want to edit single-product.php or content-single-product.php. Both will get you to the same end result.

To do the former, you can use default WordPress functionality and filter the template under given conditions via template_include

add_filter( 'template_include', 'so_29984159_custom_category_template' );
function so_29984159_custom_category_template( $template ){
    if ( is_single() && get_post_type() == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
        $template = locate_template( 'single-product-custom.php' );
    }
    return $template;
}

Or if you'd rather create a custom content-product.php template part, you can use WooCommerce's wc_get_template_part which works in pretty much the same way. Technically I think you can use the same conditional logic, but I tweaked it here to point out the variables WooCommerce makes available at the filter.

add_filter( 'wc_get_template_part', 'so_29984159_custom_content_template_part', 10, 3 );
function so_29984159_custom_content_template_part( $template, $slug, $name ){
    if ( $slug == 'content' && $name == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
        $template = locate_template( 'content-product-custom.php' );
    }
    return $template;
}

In either case the single-product-custom.php or content-product-custom.php would be in your theme's root folder. To put them somewhere else, just change the path in the locate_template() argument.

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • I'll check it out thanks for this detailed answer, hope it will work – Ben May 01 '15 at 11:30
  • I didn't test anything so be wary of stray ; or ), but I think the idea should work. – helgatheviking May 01 '15 at 13:20
  • My custom template was not well made (directly edited content-single-product.php), so I've created a content-single-product-custom.php file. And I replaced wc_get_template_part( 'content', 'single-product' ); in single-product.php to load the template for the category named custom. Better organisation but same results: example.com/product/myproduct (the product page) displays the product with custom template, but woocommerce product_page shortcode don't. Your code: Not sure where I should add this filter (template loader files?) not used to hooks and filters, I only use them since few days – Ben May 01 '15 at 15:00
  • If you use my second filter you don't need to replace `wc_get_template_part( 'content', 'single-product' )` at all. Either code snippet belongs in your theme's `functions.php`. If you need more help understanding filters I wrote what I think is a pretty good [tutorial on how to use filters](http://www.kathyisawesome.com/?p=455) – helgatheviking May 01 '15 at 16:36
0

You can not do coding in wordpress's file i.e. page.php to modify woocommerce pages. Its wrong way. You simply override WooCommerce pages into your theme folder and then modify any pages which you want to do. All templates/pages are located in a woocommerce/templates folder.

Here you can find documentation.

 http://docs.woothemes.com/document/template-structure/
Shashank Singh
  • 1,280
  • 12
  • 23
  • Yes I know I made a mistake in my question, I wrote my custom template in content-single-product.php, not in page.php – Ben May 01 '15 at 11:18
0

edit woocommerce/templates/content-single-product.php for single product view

Hardik Patel
  • 83
  • 1
  • 7
  • This is what I did, and it works, products of specific category I chose have a different template. The problem is that I want to display these products on a wordpress page. But when I use woocommerce produc_page shortcode to do so, surprisingly (well it surprised me), it doesn't diplay the products with my custom template. In other words: http://www.example.com/product/myproduct/ display the product with my custom template (located in content-single-product.php), but when I try to display the product on another wordpress page with woocommerce shortcode, it doesn't work. – Ben May 01 '15 at 11:26
  • i understand your problem. do one thing in content-single-product.php add if condition for templates. for example if you want to display differnet styple for product under XYZ template then add." if ( is_page_template('XYZ.php') ) ". you can write specific code inside it for that template. – Hardik Patel May 01 '15 at 11:36