Skip Navigation

[Resolved] Assign a template to the products pages of a specific category

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

Problem:
Assign a template to the products pages of a specific category

Solution:
You can use the view/block filter hook "wpv_filter_force_template" to assign content template dynamically for single product posts based on the taxonomy term or product category.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/assign-a-template-to-the-products-pages-of-a-specific-category/#post-1990011

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

This support ticket is created 4 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by carlC-4 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1988899

Tell us what you are trying to do?
Hi, I'm trying to assign a product page template automatically based on the product category the product is assigned to. I have 1 main template I use for nearly all product except for 1 category. I've found a few threads online already. The problem is I don't have too much experience with api web hooks.

I think this is the correct web hook to use:

add_filter( 'wpv_filter_force_template', 'my_callback_function', 99, 3 );

How do I add this to my site using the Custom code snippets in the toolset plugin. Also, we use woocommerce.

the product category slug is: promotional-products-range tag ID: 511

The template slug is: promotional-products-template ID: 12687

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1988919

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes, the hook you mention is the correct hook to use in this case.

Can you please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_force_template', 'func_assign_ct_dynamically', 99, 3 );
function func_assign_ct_dynamically( $template_selected, $post_id, $kind ) {

if( is_product() and has_term( 511, 'product_cat' ) ) {
            $template_selected = 12687; 
}
    return $template_selected;
}

The above example is considering that you have term 511 available with taxonomy product_cat. Then it will assign the content template (ID) 12687 to that specific single product post.

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

#1988939
promo products template.PNG
content template.PNG

Hi, I've added the code but the product template has not changed for the products pages. Could this be due to the other template being assigned to the Products (single)?

#1988957

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is strange.

Can you please share what content template you want to assign to what category when single product post is displayed on the frontend.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1990011

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've deactivated the Toolset layouts plugin as you were not using it. Please note that Toolset Layouts plugin is in maintenance mode and we are not going to add any new features to it. if you are not using it, please delete that plugin. I just deactivated it.

I've added the static text "template called" to your content template at top:
=> hidden link

Then, I can see the code you added to "Custom Code" section:

add_filter( 'wpv_filter_force_template', 'func_assign_ct_dynamically', 939, 3 );
function func_assign_ct_dynamically( $template_selected, $post_id, $kind ) {
 
if( is_product() and has_term( 511, 'product_cat' ) ) {
  			return $template_selected = 12687; 
}
    return $template_selected;
}

Now, when I load the test page on frontend: hidden link
I can see that "template called" text at top, that means the code is working and 12687 template ID is assigned to test product post.

#1990283

My issue is resolved now. Thank you for your help!