Skip Navigation

[Resolved] Different Single Layout Templates of Product WooCommerce depending on Category

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

Problem:
How to assign different Layout to different posts belongs to different category taxonomy of same post type

Solution:
You can use Layout's filter hook "get_layout_id_for_render" to change the layout on fly.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/different-single-layout-templates-of-product-woocommerce-depending-on-category/#post-913876

Relevant Documentation:

This support ticket is created 6 years, 7 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 4 replies, has 2 voices.

Last updated by Nashaat 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#913077

Hi,

I have this kind of product CATEGORIES in WooCommerce
1- New Cars
2- Used Cars
3- Rental Cars

LAYOUTS:
1- New Cars Layout
2- Used Cars Layout
3- Rental Cars Layout

So i have 3 different Layouts for each Category

I would like to get the Layout displayed depending on the Category. If a product assigned to "New Cars Category" then i would like to show this product with "New Cars Layout"

I have found already a solution with GUI Condition


[wpv-conditional if="( has_term('newcarcat', 'product_cat', null) eq '1' )" ]
<div class="col-12 p-5 bg-success">
  <p class="h3 p-2 text-center text-white"> THIS IS DETAILS ABOUT CAR FROM NEW CAR CATEGORY</p>
</div>
[/wpv-conditional]

[wpv-conditional if="( has_term('usedcarcat', 'product_cat', null) eq '1' )" ]
<div class="col-12 p-5 bg-dark">
  <p class="h3 p-2 text-center text-white">THIS IS DETAILS ABOUT CAR FROM USED CAR CATEGORY</p>
</div>
[/wpv-conditional]

[wpv-conditional if="( has_term('rentalcarcat', 'product_cat', null) eq '1' )" ]
<div class="col-12 p-5 bg-danger">
  <p class="h3 p-2 text-center text-white"> THIS IS DETAILS ABOUT CAR FROM RENTAL CAR CATEGORY</p>
</div>
[/wpv-conditional]

I can show/hide content on the front end depending on the category is chosen. But all this will be saved in only one layout.
Is it possible to make different Layouts for different products as i described ? even if code needed!

I tried to follow this ticket but i couldnt manage it
https://toolset.com/forums/topic/set-specific-layout-on-specific-post-status/

#913372

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - The another link you shared which includes the example to use the filter get_layout_id_for_render is the correct filter to switch the layout.

Could you please tell me how you use that filter and how you are checking or what could be the flow to check your taxonomy terms?

If will be great if you can provide all required information with problem URL.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#913546

Minesh
Supporter

Languages: English (English )

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

Well - I need FTP access details as I may need to debug issue with your code and put breakpoints.

Could you please send me working FTP access details.

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

#913876

Minesh
Supporter

Languages: English (English )

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

Could you please check now. I've modified the code hook as given under:

function assign_layout_by_post_status( $id, $layout ){
        global $product;
				
		$product_cats = get_the_terms( $product->ID, 'product_cat');
		
		if(is_array($product_cats)){
				foreach($product_cats as $k=>$v):
					if($v->slug == 'rentalcarcat'){
						$id = 4573;
					}else if($v->slug == 'usedcarcat'){
						$id = 4574;
					}
				endforeach;
		}
			
        return $id;
}
 add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);

You will see the different layouts will be assigned automatically when you view Rental, Used or New car.

Could you please confirm it works as your end as well.

#913989

Thank you Minesh your code works exactly as needed love your support! many Thanks!