Home › Toolset Professional Support › [Resolved] Store calculate shortcode result as field value
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 27 replies, has 2 voices.
Last updated by jamesR-13 1 year, 7 months ago.
Assisted by: Minesh.
I don't believe it is neccesary at this point to loop through the view as I am only trying to calculate the Quantity * Price for one Special Order Item at a time.
I will want to loop through the posts and add those values together for a grand total at a point in the future, but right now I am just trying to store the line item total for each individual Special Order Item in a field on that single post. If the item is not of type Special Order, then there is no need to store a value for the Line Item Total.
Sorry but what you explain is completely contrary.
With the following requisition: hidden link
On edit page of the above requisition I see three connected (related) posts and all are set to "Special Order":
=> hidden link
So, is that true that you want to make total of all three posts for fields (Quantity * Price) and then store that all three posts total to the "Line item total =" field?
I also wonder that the field Type displays only one time where we can select "Special Order", if you want to store line item total then this field also be in loop but its not as you are displays three "special orders". To give solution to just add the two custom fields value is really easy but here where I'm stuck is how you setup your structure and how you want.
I hope you understand my point and guide me with the right direction.
With the following requisition:
=> hidden link
I just want to know that you want to make total of all three posts as all those three posts is set as "Special Order"? or please correct me if its not.
No.
Each Special Order Item is a "Line Item" therefore I just need to store the result of the following:
req-item-quantity * special-item-price
in the custom field:
special-item-line-total
For THAT SINGLE Req Item post, that is created when the form with ID 1269 is submitted.
IN THE FUTURE I will want to store a total for multiple Req Item posts in the loop BUT THAT IS NOT WHAT I AM TRYING TO DO RIGHT NOW.
RIGHT NOW, I just want a SUBTOTAL FOR EACH INDIVIDUAL Req Item BY ITSELF to be stored in the field special-item-line-total. The subtotal value is equal to req-item-quantity * special-item-price FOR THAT SINGLE POST.
I DO NOT NEED TO DO ANYTHING WITH OTHER POSTS IN THE LOOP RIGHT NOW. ONLY THE POST CREATED WHEN FORM ID 1269 IS SUBMITTED IS OF CONCERN FOR THIS TICKET.
Please do not take my all caps writing as me yelling, I am just using all caps for emphasis. I hope we can be on the same page now. I am very close to the solution, the code starting on line 203 THROUGH LINE 211 of my functions.php should do what I am trying to do but it is not working. The code should do the following:
special-item-line-total = req-item-quantity * special-item-price
I have the following shortcode as an example of what I am trying to accomplish, the shortcode produces the correct result, I just need it done in PHP so that the result can be stored in a custom field:
[calculate] [types field="special-item-price"][/types]*[types field="req-item-quantity"][/types][/calculate]
For simplicity sake, let's only use requisition 7700 for reference
hidden link
The only form we are concerned with is the one that has the BLUE BUTTON LABELLED "ADD". That is form 1269.
Let's not look at any other requisitions, as 7700 is there for testing purposes. We also don't need to do anything with any other buttons besides the BLUE ADD button, as they will change the status of the requisition which affects the visibility of various elements of the template.
Now that is clear that you want to submit the form when we click on the "Add" button on the following requisition post and the form ID is 1269.
=> hidden link
The thing is that - you have one to many post relationship between post types Requisitions [0 .. 1] << Req Items [*] where Requisitions is parent (one) and Req Items is child many.
Now, as there is one to many post relationship between requisition and Req Items, with the following post:
- hidden link
When I edit that, I see three related items on edit page and all those related items post are set as "Special Order":
- hidden link
What you offer me is this post where I/user can add the quantity and from the Type dropdown I/user can select "Special Order".
The issue here is when we select "Special Order" from Type dropdown there is no way to know for what related item post the "Special Order" is about as you have three. I understand everything that you added the code for the form ID 1269 so when we click on "Add" button it should respect the code you added and calculate based on the formula you shared:
special-item-line-total = req-item-quantity * special-item-price
I just do not have information when I/user select "Special Order" from where I can get the "special-item-price" (I know there are three related posts available) but how to know from what post I have to grab the "special-item-price" and I assume I have to take the quantity what is added by user in the "Quantity" box.
We are really near if you just help me to solve this confusion as there is no automatic way to get the "special-item-price" when you select the "Special Order".
The shortcode you shared is used to work because you are in the view's loop and in the view's loop we get the current post in the loop and based on the current post in loop you get the "special-item-price" (or many other fields value as well) and you calculate the special-item-line-total value.
I hope you understand now where we stuck and guide me in the right direction.
The user will enter a value in the "special-item-price" field. Users are already trained to do this.
It seems you are not getting what I shared.
You want to calculate based on the following formula:
special-item-line-total = req-item-quantity * special-item-price
So, with the form you have:hidden link
- User has luxury to input req-item-quantity and with the Type dropdown user can select "Special Order" but there is no special-item-price.
When user click on "Add" button that will create a new post using the form ID 1269 but to calculate "special-item-line-total" we will require req-item-quantity * special-item-price, so we have req-item-quantity but not the special-item-price.
And we how to know what special-item-price we need to take to make calculation as there is no automatic way to get that.
There definitely IS a field for special item price. It is labelled as simply "Price" on the frontend.
I've attached a screenshot of the relevant code for form 1269, and another screenshot showing the field on the frontend.
Can you please check now: hidden link
I've added the following custom JS code to your form's JS box:
=> hidden link
jQuery(document).ready(function($){ $(document).on( 'change keyup paste', 'input[name="wpcf-special-item-price"],input[name="wpcf-req-item-quantity"]', function () { res = $('input[name="wpcf-special-item-price"]').val() * $('input[name="wpcf-req-item-quantity"]').val(); $('input[name="wpcf-special-item-line-total"]').val(res); }); });
I can see its making the total and total is also added to the above table.
Well, that does what I needed done! Thank you Minesh!
Now that that is resolved I will want to do a total of all special item line totals for the entire requisition, but I will open a new ticket for that.
I found a slight issue with the provided solution. It uses more than two decimal places. To correct the issue I added some math to the script to return the correct decimal number.
jQuery(document).ready(function($){ $(document).on( 'change keyup paste', 'input[name="wpcf-special-item-price"],input[name="wpcf-req-item-quantity"]', function () { result = $('input[name="wpcf-special-item-price"]').val() * $('input[name="wpcf-req-item-quantity"]').val(); result = Math.round(result * 1e2) / 1e2; result = result.toPrecision(6); $('input[name="wpcf-special-item-line-total"]').val(result); }); });
I used 6 numbers in the toPrecision parameter so that the math works for prices in to the thousands of dollars, as there won't be anything requisitioned that costs more than that.
My issue is resolved now. Thank you!
Well, I THOUGHT i had the decimal place issue solved, but it now forces every number to have 6 digits, even when that means there are more than two after the decimal. I have tried a number of things to resolve this so that the prices are formatted with two decimal places every time, but I am stuck. Can you help me with this please, Minesh?
Nevermind again, I found the following solution:
jQuery(document).ready(function($){ $(document).on( 'change keyup paste', 'input[name="wpcf-special-item-price"],input[name="wpcf-req-item-quantity"]', function () { qty = $('input[name="wpcf-special-item-price"]').val(); price = $('input[name="wpcf-req-item-quantity"]').val() result = (qty * price).toFixed(2); $('input[name="wpcf-special-item-line-total"]').val(result); }); });