Skip Navigation

[Resolved] Shortcode within a Conditional displays when condition is both true and false

This support ticket is created 5 years, 1 month 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: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Anthony 5 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1368863
Product-variation-conditional-shortcode.191024.png
Product-variation-conditional-field-display-problem.191024.png

Using the Block editor on a Content Template in a WooCommerce site, I'm using a Conditional to check whether the post type is 'product_variation' in order to display two custom fields ("'width' x 'height'") only when the condition is 'false' - when the product does not have variations. However, the two custom fields also display when the product does have variations.

Link to a page where the issue can be seen: hidden link

#1369587

Hi Anthony,

Thank you for contacting us and I'd be happy to assist.

I tried to view the page that you've shared, but it is showing a white blank screen with an "internal server error 500" in the browser's console.

Unfortunately, the error 500 is a very generic error and can be caused by a number of reasons, ranging from corrupted ".htaccess" file, outdated/incompatible code in plugins or themes or due to lower server resources available than required, etc.

To start troubleshooting, I'll recommend checking your hosting's error logs for any related errors or warnings. Please make sure that WordPress debugging mode is turned on.
( ref: https://codex.wordpress.org/Debugging_in_WordPress )

For more detailed troubleshooting steps, you can also read:
hidden link

Once the "error 500" is fixed, you're welcome to share temporary admin login details, so that I can see how and where the conditional shortcodes are being used,

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1370307

Hi Anthony,

Thank you for sharing the admin access.

I noticed that you're using this conditional code block in the archive for the product categories, which will only show "Products" post with post type "product".

This is why the condition to check product type is always true (as post type is never equal to "product_variation").

For such a condition, you'll first need to register a custom shortcode, that checks for the product's variations and then return the output accordingly, which can be used in the conditional code block.
( ref: https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/ )

Example:


add_shortcode("check_product_variations", "check_product_variations_func");
function check_product_variations_func($atts) {

	$a = shortcode_atts( array(
		'id' => ''
	), $atts );

	if( (!empty($a['id'])) && (is_numeric($a['id'])) && (class_exists('WC_Product_Variable')) ) {

		// get product's variations
		$product = new WC_Product_Variable( $a['id'] );
		$variations = $product->get_available_variations();

		if(sizeof($variations) >= 1) {
			// if product has a variation, return 1
			return 1;
		}
		else
		{	
			// else return 0
			return 0;
		}
	}

	// else return 0
	return 0;
}

The above code can be included in the active theme's "functions.php" file and also add "check_product_variations" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

This shortcode will accept an ID of the product and will return 0 if no variations exist and 1, if at least 1 variation exists.

Example:


[check_product_variations id="[wpv-post-id]"]

This is how it can then be used in the conditional code blocks:
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/ )


[wpv-conditional if="( '[check_product_variations id='[wpv-post-id]']' eq 1 )"]
This product has some variations
[/wpv-conditional]

[wpv-conditional if="( '[check_product_variations id='[wpv-post-id]']' eq 0 )"]
This product has no variation
[/wpv-conditional]

I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

Important note: If you experience the error 500 issues after updating to the latest WPML version again, please report this at our WPML support forum ( https://wpml.org/forums ), for further investigation.

regards,
Waqar

#1371305

My issue is resolved now. Thank you!