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]
??
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.
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');
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]
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.
Hi christian,
I have deactivated the calculation shortcode and added the fields to custom layout as your request
image attached
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]
This works fine. so i have a Total :700
but i actually want a minus calculation
_regular_price - _sale_price = 100
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]
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]
No, this shortcode is for addition and subtraction only.
Got it! Thanks for your support Christian.