I wrote the following to calculate monthly payments on the product page. Basically it requires an admin fee to be added if the loan amount is above or below 5000, the price will be increased of 99 or 49 dollars respectively.
I then calculate the monthly payment on at 12.99% over 36 months and output it on the product landing page.
I'm using get_post_meta(get_the_ID(), '_regular_price', true);
to pull in the price of the product.
<?php
function FinanceCalc() {
function AddAdminFee() {
$a = get_post_meta(get_the_ID(), '_regular_price', true);
if ($a >= 5000) {
return $a + 99;
} else {
return $a + 49;
}
}
$loanamount = AddAdminFee();
function calcPmt( $amt , $i, $term ) {
$int = $i/1200;
$int1 = 1+$int;
$r1 = pow($int1, $term);
$pmt = $amt*($int*$r1)/($r1-1);
return $pmt;
}
$payment = calcPmt ( $loanamount, 12.99, 36 );
return round($payment*100)/100;
}
$monthlypayment = FinanceCalc();
?>
I then call in the outputted price as below. It's limited to a certain category as not all products require this calculator.
<?php if ( has_term ( 'the-category-term-here', 'product_cat')) {
echo 'Finance this for $' . number_format($monthlypayment, 2) . ' per month';
}
?>
I've placed all this code on the content-single-product-default.php
and it works. When I try to do this on content-product.php
as part of the loop on a taxonomy result I get the following error:
Cannot redeclare FinanceCalc() (previously declared in .../content-product.php:100) in .../content-product.php on line 131
Any clue what I'm doing wrong? Any suggestions also on how to clean this up and if there's a better way?
I've written this by just messing around with php, using simple math and Google.
I'm surprised there's no plugins available for this.