Skip Navigation

[Resolved] Displaying a weight as pounds at 3 and above and ounces below 3

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
- 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 1 reply, has 2 voices.

Last updated by Waqar 1 year, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2506303

Tell us what you are trying to do? My CPT includes a field for weight in pounds. It currently displays as '2lb'. The client would like all weights below 3lb to display in ounces. This requires multiplying the value by 16 if the value is less than 3 and then appending 'lb' at 3 and above and 'oz' below 3.

What is the most efficient way to do this?

Is there any documentation that you are following? I couldn't find a way to add a field that would automatically calculate its value from the 'weight' field value.

I couldn't figure out how to perform the calculation within a conditional in Views.

Is there a similar example that we can see? n/a

What is the link to your site? hidden link

Admin access is still in place from prior tickets.

#2506559

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

To achieve this, you can register a custom shortcode, that gets the value from the weight custom field and then return the output, after the necessary calculation and suffix.

For example:


add_shortcode('custom_show_weight_field', 'custom_show_weight_field_func');
function custom_show_weight_field_func() {
	
	$weight_field_slug = 'recipe-weight';

	$field_value = types_render_field($weight_field_slug, array("output" => "raw"));

	if($field_value >= 3) {
		$final_value = $field_value.'lb';
	}
	else
	{
		$final_value = ($field_value*16).'oz';
	}

	return $final_value;
}

This shortcode uses a slug 'recipe-weight' for the weight custom field, but you can change it as per your website.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, where you'd like to show this field's output, you can use the shortcode like this:


[custom_show_weight_field]

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

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