Skip Navigation

[Resolved] Calculate Woo Fields

This support ticket is created 5 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 3 voices.

Last updated by Nashaat 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#912952

Hi,
I am trying to calculate WooCommerce Price Field and On Sale Field
[wpv-woo-product-price]-[wpv-woo-onsale]= Savings
40000-30000=10000

I have added this function to Code Snippet. this works good only with type fields

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

This code for rendering the results on the front end

[calculate][types field="normal-price"][/types]-[types field="our-price"][/types][/calculate]

how can i get this working with woocommerce fields too?i dont know what is the right way to add woocommerce field to above code

[calculate][wpv-woo-product-price]-[wpv-woo-onsale][/calculate]
??

#912990

First, please remove that code and use something more safe.
Eval should not really be used unless we really know what we do.

I suggest something like this below.

1. Register this 2 ShortCodes:

global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
 
$total += wpv_do_shortcode($content);
}
 
add_shortcode('add-to-total', 'add_total_shortcode');
function show_total_shortcode() {
global $total;
$totalNew = $total;
$total = 0;
return $totalNew;
}
 
add_shortcode('show-total', 'show_total_shortcode');

2. Register those ShortCodes in Views > Settings > Compatibility > 3rd Party ShortCodes

3. Use the ShortCodes in a View as this:

<wpv-loop>
    [types field="your-numeric-field" output="raw"][/types]
    [add-to-total]
      [types field="your-numeric-field" output="raw"][/types]
    [/add-to-total]
</wpv-loop>
[show-total]

==> You can insert any Custom Field there that returns the right values.
For non-types Fields, you can always use the "non-Types" fields section in the Fields and Views GUI.

#914887

Hi Beda,
I have solved this problem this way
I have added two fields "_regular_price" and "_sale_price" in Manage non-Toolset Post Fields with Toolset Forms.

Then i have added them into my single Layout of product like following:

[calculate][wpv-post-field name="_regular_price"]-[wpv-post-field name="_sale_price"][/calculate]

This works good for now. but do you mean the function i used for calculating is not ok?

function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
return round($content);
}
add_shortcode('calculate', 'calculate_shortcode');
#914894

I have tried using your code but unfortunately this didnt work

[wpv-post-field name="_regular_price"]
    [add-to-total]
      [wpv-post-field name="_sale_price"]
    [/add-to-total]
[show-total]
#914942

Hi, can you temporarily remove the custom shortcodes and confirm the field values?

Regular price: [wpv-post-field name="_regular_price"]<br />
Sale price: [wpv-post-field name="_sale_price"]<br />

Please tell me what you see for each of these fields and we can go from there.

#914949
Fields Value.png

Hi christian,
I have deactivated the calculation shortcode and added the fields to custom layout as your request

image attached

#914994

Okay thanks, if you want to add these two field values, both should be wrapped in add-to-total shortcodes like this:

[add-to-total][wpv-post-field name="_regular_price"][/add-to-total]
[add-to-total][wpv-post-field name="_sale_price"][/add-to-total]

Total: [show-total]
#915101

This works fine. so i have a Total :700
but i actually want a minus calculation
_regular_price - _sale_price = 100

#915353

Add a negative sign to the sale price value like this:

[add-to-total][wpv-post-field name="_regular_price"][/add-to-total]
[add-to-total]-[wpv-post-field name="_sale_price"][/add-to-total]
 
Total: [show-total]
#915839

Thanks Christian. this works now.

I wonder if its possiblte to make more calculation using * or /?

i tried following but this doesn't work

[add-to-total][wpv-post-field name="_regular_price"][/add-to-total]
[add-to-total]*[wpv-post-field name="_sale_price"][/add-to-total]
  
Total: [show-total]
[add-to-total][wpv-post-field name="_regular_price"][/add-to-total]
[add-to-total]/[wpv-post-field name="_sale_price"][/add-to-total]
  
Total: [show-total]
#915946

No, this shortcode is for addition and subtraction only.

#916200

Got it! Thanks for your support Christian.

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