Aleš Sýkora wrote

Calculate between Toolset Types fields easily

10. 10. 2018 in

I have a fast tip for you today. If you need some basic math operations between your custom fields made with Types plugin, then I have a solution for you.

I need to display my custom field with price without VAT and then calculate the price with VAT. In Czech republic, VAT is 21%. I need to multiply the “price-without-vat” field with number 1.21. How can I do that?

First thing I need is my own plugin (of course you can add the function to functions.php but i don’t recommend that).

I need header info for my plugin:

<?php
/**
 * Plugin Name: Calculations Shortcode
 * Description: Add shortcode calculate
 * Plugin URI: 
 * Author: Aleš Sýkora
 * Author URI: http://www.alessykora.cz/
 * Version: 1.0.2
 * License: GPL2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

And function for caculations:

add_shortcode('calculate', 'calculate_shortcode');
function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval('return ' . $content . ';');
return $content ;
}

Version with rounding:

add_shortcode('calculate', 'calculate_shortcode');
function calculate_shortcode($atts=[],$content=null) {
$atts = shortcode_atts(['round' => 0], $atts );
$content = wpv_do_shortcode($content);
$content = eval('return ' . $content . ';');
$content = round($content, $atts['round']);
return $content;
}

Save the file with .PHP suffix and add it to the .ZIP file. Then open your WordPress admin and install the plugin.

activate wordpress plugin

You can download:

Plugins do not work together.

After Instalation and activation of the calculations plugin go to the Toolset settings and register the calculate shortcode

Toolset > Settings > Front-end Content > Third-party shortcode arguments

third party shortcode integration views

Now it’s time to use the shortcode in view. Open your view and use it like this:

[calculate][field 1 raw]+[field 2 raw][/calculate]

Or like me, if you need to use only only Custom field:

[calculate][field 1 raw]*1.21[/calculate]

With new calculations round plugin use round option (if you need). If you do not fill the number, then it will round to 2 decimals.

[calculate round=2][field 1 raw]*1.21[/calculate]

In my case it finally looks like this:

calculate view custom fields

If your field is empty, then the plugin fail in case of empty_field * 1.21. So be sure to have conditional for displaying the calculation.

Did you successfully implemented your custom calculate shortcode in search form? Let me know in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Featured products query WooCommerce
11. 4. 2021 in
If you are using your own queries for WooCommerce stores, for example in Oxygen builder’s custom code block then you may need query for featured products. You can use the query I am using inside my product sliders :-).
Read More
Aleš Sýkora wrote
Disable Woocommerce image zoom on hover in oxygen
19. 11. 2020 in ,
Add this to custom snippets. Or put in custom funcionalities plugin. Dont worry, lightbox will be working. Then the magnyfing glass icon dissapears, the over effect is gone, but the lightbox works perfect :-).
Read More
Aleš Sýkora wrote
Modify ‘ASAP’ label in Iconic Delivery Slots
5. 1. 2021 in
You can use this code snippet to change the label for 'ASAP' when using ASAP delivery.
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram