Skip Navigation

[Resolved] How to create and link product templates to product types

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to use a different template for each WooCommerce product type.

Solution:
WooCommerce product types is a hidden taxonomy, but we can still pull the value of a product using Toolset.

We can assign ONLY ONE content template per custom post type(product). We can then use conditional shortcodes on product_type taxonomy and customize the content of the conditional shortcode, or create an unassigned content template per product type and use it inside the conditional shortcodes, check this example:

[wpv-if evaluate="has_term('simple', 'product_type', null) = '1'"]
[wpv-post-body view_template="simple-product-template"]
[/wpv-if]
[wpv-if evaluate="has_term('variable', 'product_type', null) = '1'"]
[wpv-post-body view_template="variable-product-template"]
[/wpv-if]

Relevant Documentation:

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by GinaM9227 4 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1580055
content_templates1.jpg

Tell us what you are trying to do?
I want to create different templates for different Product Types. Right now I have only one Product Content Template - see attached screenshot.
How do I create new Templates for Product Types? or just use the Basic Woo Product Type.
And how do I make it link to the different products. For example, the Virtual Product would be assigned automatically a template and the Subscription would have a template.

Is there any documentation that you are following? None

Is there a similar example that we can see? Just to be able to sell different products and have templates for different product types, not just one.

What is the link to your site? hidden link.

#1581087

Hello and thank you for contacting the Toolset support.

I believe Product types are handled by custom taxonomy product_type with the following default terms:
- simple
- grouped
- variable
- external
Virtual, on the other hand, is stored as a custom field.

Based on that I can suggest two scenarios:
- Using conditions on product type to show different output/content_template https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/
- Hooking into "wpv_filter_force_template" and assigning the correct content template based on product type https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

1. Using conditions
- You can use one content template and show/hide sections depending on product_type.
- Use a different content template for each product type. Something like:

[wpv-if evaluate="has_term('simple', 'product_type', null) = '1'"]
[wpv-post-body view_template="simple-product-template"]
[/wpv-if]
[wpv-if evaluate="has_term('variable', 'product_type', null) = '1'"]
[wpv-post-body view_template="variable-product-template"]
[/wpv-if]

2. Custom code
Check this example:

//Show a specific Content Template applied to a given post for not logged in visitors and in every place where this post appears:
add_filter( 'wpv_filter_force_template', 'prefix_fixed_content_for_visitors', 99, 3 );
  
function prefix_fixed_content_for_visitors( $template_selected, $id, $kind ) {
    if ( !is_user_logged_in() && $id == 345 ) { // if the user is not logged in and is trying to view the post with ID 345
        $template_selected = 123; // assign a fixed Content Template with ID 123 that contains a static text
    }
    return $template_selected;
}

You will need to create conditions on the post_type and also the custom field for virtual products.

I hope this helps. Let me know if you have any doubts.

#1589085
Custom_post_taxonomy.jpg

Thank you for your answer.

Basically: I have a Master Content Template for Products. There are Field Groups that are appearing but that should be assigned to specific taxonomies. For example: The Field Group called Events Extra would only apply for Products that are Event Tickets but right now it is showing up for all Product types.

I'm trying to assign Product Field groups to specific taxonomies. How to make this Field Group only appear when the Product Type Event Tickets is selected? Per the attached screenshot, I do not see the custom taxonomy product_type that you mention.

Thank you

New threads created by Jamal and linked to this one are listed below:

https://toolset.com/forums/topic/custom-fields-groups-for-specific-product-types/

#1589475

Hi,

Indeed, the code that I added was to give an example of how we can use the "wpv_filter_force_template" filter. If you are not comfortable with PHP coding, I would suggest the 1st solution using conditions on the content template.

[wpv-if evaluate="has_term('simple', 'product_type', null) = '1'"]
[wpv-post-body view_template="simple-product-template"]
[/wpv-if]
[wpv-if evaluate="has_term('variable', 'product_type', null) = '1'"]
[wpv-post-body view_template="variable-product-template"]
[/wpv-if]

However, for support rules, we are able to handle only one issue at the time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I created another ticket for your last question, and I'll be relying on it.

Let us focus on this ticket on your original question "use a different content template for each product type". If you think this was answered, please mark this ticket as resolved.

#1589509

I am not familiar with coding so did not quite understand the "wpv_filter_force_template" suggestion..need to review..

About the suggestion of using conditional to show fields inside the templates ( [wpv-post-body view_template="simple-product-template"] and [wpv-post-body view_template="variable-product-template"] )

The issue is that I only have One Template - I don't have one for Simple Product nor do I have one for Variable Product..if I use that code, what would be the default template showing up for different product types.

Thank you

#1589627

We can assign ONLY ONE content template per custom post type. We cannot assign more than one. This means, that all your products(simple, variable, etc) can only use one content template, let's call it "Single Product Page Content Template".

But you can still create an unassigned content template, just do not assign it to any post type. So, we can create 2 content templates:
- "Simple Product Template" with slug "simple-product-template".
- "Variable Product Template" with slug "variable-product-template".

Display different styles, fields, forms, views on each template as you would like.

Then on the assigned template "Single Product Page Content Template", use the conditional shortcodes and insert a content template on each condition.

If you do not want to create a content template per product type, you can customize your default content template for each product type inside the conditional shortcodes.

#1589881

I'm beginning to understand. The 3rd party plugin that I am using has created a product type (Event Tickets) that is in addition to the standard Woo products. The Event Tickets product has fields that I'd like to Hide on the Front End if the product type is not Tickets.

So how do I find out what is the code for the Event Ticket product type. With this information, I could create a conditional that would hide the field if it has the word "event_tickets".

I checked the custom fields available on the post and tickets was not on the list and neither is it on the hidden fields..

New threads created by Jamal and linked to this one are listed below:

https://toolset.com/forums/topic/customize-template-based-on-the-custom-field/

#1589963

For support rules, we are able to handle only one issue at the time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I created another ticket and I'll be replying to it.

If you think that the original question of this ticket is answered, please mark it as resolved.

#1589983

My issue is resolved now. Thank you!