Skip Navigation

[Résolu] Different template for different products

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to use the "Custom Product Boxes" plugin with custom product pages created with Toolset?

Solution:
The Custom Product Box plugin needs to use the standard WooCommerce product template to function correctly, and not the WooCommerce Views template used for creating custom product designs.

There is a workaround described in the main thread below to be able to switch between each depending on whether the product is a normal product or a Custom Product Box product.

This support ticket is created Il y a 5 années et 7 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 réponses, has 2 voix.

Last updated by Nigel Il y a 5 années et 7 mois.

Assisted by: Nigel.

Auteur
Publications
#1108259

Hi Support Team,

I have a Toolset site using Woocommerce and Woocmmmerce Views.

I set this up nicely to use the "WooCommerce Views plugin default single product template" and a content template for products.

Now I'm trying to integrate a plugin
hidden link

This creates a new product type "Custom Product Box" and displays it in a certain way. But when I have my Woocommerce views set up as above it just displays these products as normal products via the content template.

If I switch back to "WooCommerce Plugin Default Templates" in Woocommerce Views settings then the Custom Boxed Products are displayed as they should be, but the normal products are displayed as Woocommerce default.

Is there a way to make it select the theme template dependent on product type?

i.e.
If "Custom Boxed Product" use "WooCommerce Plugin Default Templates"
Else use "WooCommerce Views plugin default single product template"

Or, failing that, is there a way to get the Custom Product Boxes output into my Content Template?

Is there any documentation that you are following?
None specifically - I've searched and tried a few things but none that worked.

Is there a similar example that we can see?
Not that I know of.

What is the link to your site?
hidden link
It's in development so I will need to send yo a login to view.

#1108374

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Tim

I suspect the issue would be that the plugin uses a particular hook from the standard WC product template which isn't available in the Toolset template, but I'd need to examine this more closely to see.

Let me set up a private reply to get credentials from you if that's okay, as a I don't have access to that plugin otherwise.

#1109447

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Thanks for that.

I think the best way to handle this is to switch template based upon the product type.

Product types are stored as a custom taxonomy "product_type", and it looks like the term slug for bundle products is 'wdm_bundle_product', so that is what we can test for, and force the standard WC product template in that case.

So...

1. make a copy of the Toolset WooCommerce Views single-product.php template (from plugins/woocommerce-views/templates) and save it to a /woocommerce/ directory in your child theme folder. That way you can make changes to it and not lose them with a plugin update.
2. go to Toolset > WooCommerce Views and specify this template for single products (it will be described as your theme's custom single product template)
3. edit the file itself and replace it with the following code. You are not using Layouts, so I took the opportunity to simplify the template somewhat at the same time.

<?php
/**
 * The Template for displaying all single products.
 * Modified to add support for Toolset WooCommerce Views and Layouts
 *
 * Override this template by copying it to yourtheme/woocommerce/single-product.php
 *
 * @author 		WooThemes/OnTheGoSystems
 * @package 	WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/** Otherwise use the usual shop */
get_header('shop');	
			

/**
 * woocommerce_before_main_content hook
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 */
do_action('woocommerce_before_main_content');

/**
 * Logic to test product type
 */
global $post;
$product_type_obj = wp_get_post_terms( $post->ID, 'product_type' );
$product_type = $product_type_obj[0]->slug;
if ( 'wdm_bundle_product' == $product_type ) {
	$force_wc = true;
}

if ( defined('WC_VIEWS_VERSION') && !isset( $force_wc ) ) {

	/** WooCommerce Views activated */
	while ( have_posts() ) {
		the_post();
		the_content();
	}            		

} else {
	/** WooCommerce Views not activated, default to WooCommerce content rendering templates */
	while ( have_posts() ) {
		the_post();
		wc_get_template_part( 'content', 'single-product' );
	}           	
}

/**
* woocommerce_after_main_content hook
*
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action('woocommerce_after_main_content');

/**
* woocommerce_sidebar hook
*
* @hooked woocommerce_get_sidebar - 10
*/
do_action('woocommerce_sidebar');
		
get_footer( 'shop' );

I tested this locally, reverting to the standard WC template just for external products, and I think it should work the same for your bundle product type.

Let me know how you get on.

#1109765

Thanks Nigel, once again a perfect solution!

#1109769

Oops, I spoke too soon.

I just noticed that after doing this there's just one small problem. In the wdm_bundle_product, the description tab contains the full view layout rather than just the description field:

hidden link

Thanks
Tim

#1110505

Scratch that. I just needed to Content Template to "None" for the individual boxed product.

Thanks again for your help.

#1110515

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Tim

I spent quite a while on your site and my local test site trying to spot the problem, and it is when using the custom template I provided above, the description tab is outputting the content generated by the content template assigned to the product, rather than just outputting the post content itself (which is the field the description comes from).

I'm asking a colleague to take a look and see if they can spot why. I'll get back to you when I have news.

In the meantime, simply un-assigning the Content Template on the individual product box posts should resolve the problem.

#1110568

Thanks Nigel. That seems to work so I;m happy with the solution given - just need to make sure my client is aware of having to set Custom Template to None if he ever adds any new boxed products.

Thanks again.

#1110577

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

I asked my colleague to look into it if they have some spare time (which is currently in short supply), and if they identify a solution that doesn't involve manually unsetting the content template I'll let you know.

We have compatibility issues with 3rd party WC extensions come up from time to time and it would be good to have this as a documented option.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.