Ask your WordPress questions! Pay money and get answers fast! (more info)

Two seperate templates for woocommerce single product WordPress

  • SOLVED

I am working on a [[LINK href="http://development.ride-engine.com/shop/"]]custom woocommerce site[[/LINK]] and I would like to have categories display differently than one another.

I have been working with two methods; a conditional statements within one single-product.php, and a conditional that switches between two different files based on values. My preference would be the redirect unless for some reason it's poor practice

[[LINK href="http://pastebin.com/z42ry4H0"]]Here is my single page setup[[/LINK]]

[[LINK href="http://pastebin.com/kF5pU0HF"]]Here is my redirect setup[[/LINK]]

The first method displays the first condition, but wont display the else. The second method wont look in the right place for the file. I need to create a variable that finds the proper template directory per environment or something


My PHP skills are obviously quite poor, but I am savvy enough to understand and test things quickly - hopefully this will be a quick win for someone :)


Answers (2)

2013-12-21

Fahad Murtaza answers:

Use woocommerce_get_template


shipwreck comments:

Could you please specify where exactly?

if( has_term( 'armor', 'harnesses' ) ) {
$file = 'single-product-conditional.php';
} else {
$file = 'single-product-original.php';
}

global $woocommerce;

load_template( $woocommerce->template_url . $file );


Fahad Murtaza comments:

if (is_product_category( 'CATEGORYNAME' ) { // Change this line to your term code
woocommerce_get_template_part( 'content', 'single-product-CATEGORYNAME' );
}else{
woocommerce_get_template_part( 'content', 'single-product' );
}

instead of



if( has_term( 'armor', 'harnesses' ) ) {

$file = 'single-product-conditional.php';

} else {

$file = 'single-product-original.php';

}


Fahad Murtaza comments:

So a workflow for a solution like this is (In case you have missed something)

To edit content-single-product.php I assume you copied this file to a directory called "woocommerce" in your root theme directory. If not you should do this.

In this directory called "woocommerce" duplicate your content-single-product.php file and name the new one content-single-product-CATEGORYNAME.php*

Also copy the woocommerce template file single-product.php from woocommerce to your directory called "woocommerce" and find on line 28:

woocommerce_get_template_part( 'content', 'single-product' );

Change this to:

if (is_product_category( 'CATEGORYNAME' ) {
woocommerce_get_template_part( 'content', 'single-product-CATEGORYNAME' );
}else{
woocommerce_get_template_part( 'content', 'single-product' );
}

If you were to do this for a second category and duplicate content-single-product.php again naming the second one content-single-product-SECONDCATEGORYNAME.php then your code would look like this:

if (is_product_category( 'CATEGORYNAME') {
woocommerce_get_template_part( 'content', 'single-product-CATEGORYNAME' );
}elseif (is_product_category( 'SECONDCATEGORYNAME') {
woocommerce_get_template_part( 'content', 'single-product-SECONDCATEGORYNAME' );
}else{
woocommerce_get_template_part( 'content', 'single-product' );
}


Hope that helps.

That should answer a lot of questions and is the right way to do things. I personally hate redirecting anything.


shipwreck comments:

Fahd thank you for your detailed response. I have three files in my theme/woocommerce:

[[LINK href="http://pastebin.com/uiiGkcEd"]]single-product.php[[/LINK]]
[[LINK href="http://pastebin.com/JeqPVMbS"]]single-product-harnesses.php[[/LINK]]
[[LINK href="http://pastebin.com/Q7Lx8fcw"]]single-product-other.php[[/LINK]]

I loaded these files to [[LINK href="http://development.ride-engine.com/shop/"]]the development server [[/LINK]] so you can perhaps see what is wrong

The first issue is it doesn't appear to be finding the category, which is why I was using has_term. The second issue is how I've been displaying data on the product page by calling things like title and thumbnail directly, then letting the description and addons values be displayed with woocommerce_get_template_part. So what is happening here is two of those functions, one in single-product.php and another in single-product-harnesses - I am guessing this is not best practice



shipwreck comments:

I turned on categories in the url thinking that would help but it didn't seem to do anything


shipwreck comments:

Also when you say:

<blockquote>In this directory called "woocommerce" duplicate your content-single-product.php file and name the new one content-single-product-CATEGORYNAME.php*</blockquote>

I'm not sure what file you are referring to, as I dont see anything in the woocommerce folder with that name


shipwreck comments:

Never mind I found it, let me see what I can do with it...


shipwreck comments:

Do you mean:

if (is_product_category( 'CATEGORYNAME' ) {

woocommerce_get_template_part( 'content', 'content-single-product-CATEGORYNAME' );

}else{

woocommerce_get_template_part( 'content', 'content-single-product' );

}

So when a user goes from the archive to a single product, single-product.php loads, and then hits the conditional for category where it gets sent to content-single-product-harnesses or content-single-product


shipwreck comments:

I see that somehow single-product automatically goes to content-single-product.php, and single-product-harnesses automatically goes to content-single-product-harnesses.php. Not sure how that works but I can see that its working because I can see my content-single-product pages coming through in the else statement

My remaining issue is that the first condition wont load, for some reason the category isnt being recognized

Sorry if I am being confusing, would be nice if there was edit / delete for posts


Fahad Murtaza comments:

yes, actually I talked to Lawrence, the owner of the site and support person (Misty) has assured me that post edit will be implemented.

About your problem, I am looking into it. The site was empty for all products, so I couldn't make sense of it.


shipwreck comments:

I loaded the current setup to [[LINK href="http://development.ride-engine.com/shop/"]]the development server[[/LINK]]

This is my current code on single-product.php


<?php while ( have_posts() ) : the_post(); ?>

<?php if (is_product_category( 'HARNESSES' )) {

woocommerce_get_template_part( 'content', 'single-product-harnesses' );

}else{

woocommerce_get_template_part( 'content', 'single-product' );

} ?>

<?php endwhile; // end of the loop. ?>



Fahad Murtaza comments:

HARNESSES, should that be lower case like harnesses. Please check if harnesses is the slug for the category. Also resave your permalinks again.


shipwreck comments:

Changed harnesses to lower case, harnesses is slug for category, updated permalinks with 'shop base with category'

No difference


shipwreck comments:

This is my current code on single-product.php


<?php if (is_product_category( 'harnesses' )) {

woocommerce_get_template_part( 'content', 'single-product-harnesses' );

}else{

woocommerce_get_template_part( 'content', 'single-product' );

} ?>



'single-product-harnesses' is content-single-product-harnesses.php
'single-product' is content-single-product.php. I know this resolves because I have edited the file and can see the changes.


Fahad Murtaza comments:

OK, please do one thing, take the fresh code from

[[LINK href="https://github.com/woothemes/woocommerce/blob/master/templates/single-product.php"]]https://github.com/woothemes/woocommerce/blob/master/templates/single-product.php[[/LINK]]

and add your conditional code to line 28. I see there is a lot of updated API code for woocommerce there.

Try the original


if( has_term( 'armor', 'harnesses' ) ) {


too. But also test with the category code.


shipwreck comments:

Copied over the code, added the category conditional, didn't make a difference. I tried to change 'woocommerce_get_template_part' to 'wccommerce_get_template_part' and I get an error of 'Call to undefined function'

I noticed when I write

<?php if (is_product_category( 'harnesses' )) {

wc_get_template_part( 'content', 'single-product-harnesses' );

}else{

woocommerce_get_template_part( 'content', 'single-product' );

} ?>

<?php endwhile; // end of the loop. ?>


It doesn't throw an error, so for sure something is happening with either the is_product_category or the harness category...


shipwreck comments:

I see my url is 'http://localhost/engine/shop/harnesses/armor/' so it is definitely sorting by category. I double checked product category name and slug are 'harnesses'. Updated my permalinks. Restarted wampserver...


Fahad Murtaza comments:

That is

wc_get_template_part

not

wccommerce_get_template_part

My head is spinning :)

Inboxing you.


shipwreck comments:

Sorry, that typo was in my post not in my code. Still getting error on wc_get_template_part


shipwreck comments:

When I write


<?php while ( have_posts() ) : the_post(); ?>

<?php if ( is_product( 'armor' ) ) {

woocommerce_get_template_part( 'content', 'single-product-harnesses' );

}else{

woocommerce_get_template_part( 'content', 'single-product' );

} ?>

<?php endwhile; // end of the loop. ?>


Both the categories resolve to 'single-product-harnesses' (content-single-product-harnesses.php)

I am truly confused!


Fahad Murtaza comments:

Hi Noah

I'll be creating a test case locally and will get back to you within 24 hours.

Take a break :)

2013-12-21

Sabby Sam answers:

Hi,
Apparently , is_product() doesn't have any parameter , you need to with some other step. This will never and ever work as is_product doesn't have parameter.


Sabby Sam comments:

I guess this will work for you

[[LINK href="http://wordpress.org/support/topic/restrict-payment-options-based-on-product"]]http://wordpress.org/support/topic/restrict-payment-options-based-on-product[[/LINK]]