Home › Toolset Professional Support › [Resolved] Pulling data for monthly payment into search loop
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.
This topic contains 4 replies, has 2 voices.
Last updated by matthewS-8 1 year, 6 months ago.
Assisted by: Christopher Amirian.
Tell us what you are trying to do?
I am trying to build a function in which it calculates a monthly payment, and returns the result in a searchable loop template.
I was able to do this using php and on an individual page. Pull the Current Price value into a php snippet and doing the calculation and returning the results. But when I put this shortcode into the loop it crashes. Is this even possible?
Is there any documentation that you are following?
Yes, toolset help docs
Is there a similar example that we can see?
Page with the function working....hidden link (Scroll down below the javascript mortgage calculator
What is the link to your site?
rth.thebrandloveeffect.com
my PHP code is follows:
<?php
function percent($number){
return $number * 100 . '%';
}
function monthlyPymt() {
//$loanAmt = 584125; //amount borrowed
// get the post id and assign it to a variable
$post_id = get_the_ID();
//get the value from the field as it appears in the post with the ID from above and assign it to a variable
$fieldval = types_get_field_meta_value( 'current-price', $post_id );
//transfer the array to the loan amount (post id and current-price)
$loanAmt = $fieldval;
$intRate = 4.5; //interest rate
$term = 30; //term
$months = ( $term * 12 );
$answer = ($loanAmt * ($intRate / 100)) / 12;
$answer_two = ( $loanAmt * (($intRate/12) / 100) / ( 1 - pow( 1 + (($intRate/12) / 100), -$months)) );
// echo (types_render_field( 'current-price', array() ));
// echo (get_post_meta( $post->ID, "current-price", false ));
echo "Loan Amount $", number_format($loanAmt,2);
echo "<br>";
echo "Term ", ($term), " years";
echo "<br>";
echo "Interest Rate Percentage ", number_format($intRate,2), "%";
echo "<br>"; echo "<br>";
echo "Monthly Payments would be "; echo "<br>"; echo "<br>";
echo "Interest Only $", number_format($answer,2);
echo "<br>";
echo "<div id='results'><p class='calc_result'>Monthly Payment $", number_format($answer_two,2) , "</p></div>";
}
monthlyPymt(); // call the function
// register shortcode
add_shortcode('payments', 'monthlyPymt');?>
Ultimately, I only need to show the Monthly Payment under the Full price in the Looped View. - The mortgage calculator will be on the main linked page for users to tinker with. The interest rate, term and will be hidden on the loop page, but is showing now as a way to double check the math is correct.
hidden link
I also tried using the Formiddable forms shortcode in the loop templates, and contacted them - they said it was not supported.
Thank you for you assistance. (As usual - you guys are amazing)
Hi there,
Would you please enable the WordPress debugging to see what is the fatal error when it crashes the website using the Shortcode in the loop?
Would you please access your website files and edit the "wp-config.php" file on the root folder of your WordPress installation and add the code below into the file:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true );
Please add the code above the line below:
/* That's all, stop editing! Happy blogging. */
For more information:
https://wpml.org/documentation/support/debugging-wpml/
What I have in mind at the moment are two points:
1- Use the Global Post object as it is a loop we are talking about.
2- Try to concatenate all the output and then return it.
I came up with this code but I did not test that so it is just a suggestion of the code improvement but as long as I do not have the actual error that happens inside the loop I shoot in the dark, you know.
function monthlyPymt() { global $post; $post_id = $post->ID; $fieldval = types_get_field_meta_value( 'current-price', $post_id ); $loanAmt = $fieldval; $intRate = 4.5; $term = 30; $months = ( $term * 12 ); $answer = ($loanAmt * ($intRate / 100)) / 12; $answer_two = ( $loanAmt * (($intRate/12) / 100) / ( 1 - pow( 1 + (($intRate/12) / 100), -$months)) ); $output = "Loan Amount $". number_format($loanAmt,2); $output .= "<br>"; $output .= "Term ". $term. " years"; $output .= "<br>"; $output .= "Interest Rate Percentage ". number_format($intRate,2). "%"; $output .= "<br><br>"; $output .= "Monthly Payments would be <br><br>"; $output .= "Interest Only $". number_format($answer,2); $output .= "<br>"; $output .= "<div id='results'><p class='calc_result'>Monthly Payment $". number_format($answer_two,2). "</p></div>"; return $output; } add_shortcode('payments', 'monthlyPymt');
Thanks.
Hi CHristopher,
I've added the code, but it's not working. Debugging is on, Here's what it's reporting...I'm a php novice so I'm not sure how to resolve. Hopefully you can assist me. Thanks!
Fatal error: Cannot redeclare monthlyPymt() (previously declared in /home/brandl0v3/public_html/rth.thebrandloveeffect.com/wp-content/plugins/code-snippets/php/front-end/class-frontend.php(239) : eval()'d code:5) in /home/brandl0v3/public_html/rth.thebrandloveeffect.com/wp-content/plugins/code-snippets/php/front-end/class-frontend.php(239) : eval()'d code on line 4
(Code Added per your suggestion)
<?php
function monthlyPymt() {
global $post;
$post_id = $post->ID;
$fieldval = types_get_field_meta_value( 'current-price', $post_id );
$loanAmt = $fieldval;
$intRate = 4.5;
$term = 30;
$months = ( $term * 12 );
$answer = ($loanAmt * ($intRate / 100)) / 12;
$answer_two = ( $loanAmt * (($intRate/12) / 100) / ( 1 - pow( 1 + (($intRate/12) / 100), -$months)) );
$output = "Loan Amount $". number_format($loanAmt,2);
$output .= "";
$output .= "Term ". $term. " years";
$output .= "";
$output .= "Interest Rate Percentage ". number_format($intRate,2). "%";
$output .= "";
$output .= "Monthly Payments would be ";
$output .= "Interest Only $". number_format($answer,2);
$output .= "";
$output .= "Monthly Payment $". number_format($answer_two,2). "";
return $output;
}
add_shortcode('payments', 'monthlyPymt');
?>
Hi there,
The error shows that the function is written two times and it is in the code snippets plugin. But I am not sure.
This is a custom code matter and is outside of our support scope, but I'd be happy to take a look into the code.
I'd appreciate it if you could give me the URL/User/Pass of your WordPress dashboard after you make sure that you have a backup of your website.
It is absolutely important that you give us a guarantee that you have a backup so if something happens you will have a point of restore.
Make sure you set the next reply as private.
Please tell me where you added the code, and where you call it.
Thanks.
My issue is resolved now. Thank you! FYI - I was implementing the snippet incorrectly. Once I fixed that, it worked like a charm!! Thank you!!