1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
thisisbolo
  • 23
  • 9

1 Answers1

1

Your function code will need to be in your theme's function.php file (just one time), but not multiple times in different templates. Then you will be able to call it (execute it) multiple times in your different templates without any error message. Remember that a function can be declared just once.

Now you don't really need in your main function code childs functions, as they are not called multiple times… So your function could be wrote this way:

function FinanceCalc() {

    $price = get_post_meta(get_the_ID(), '_regular_price', true);

    $loanamount = $price >= 5000 ? $price + 99 : $price + 49;

    $i = 12.99;
    $term = 36;
    $int = $i / 1200;
    $int1 = 1 + $int;
    $r1 = pow($int1, $term);

    $payment = $loanamount * $int * $r1 / ($r1 - 1);

    return round($payment * 100) / 100;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Now in your template file you can call it and execute simply this way:

<?php if ( has_term ( 'the-category-term-here', 'product_cat')) {
    $monthlypayment = FinanceCalc();
    echo 'Finance this for $' . number_format($monthlypayment, 2) . ' per month';
} ?>

And you can call FinanceCalc() function and execute it in your other template file in a similar way…


Update: Limit the display to a certain price amount (related to your comment):

<?php if ( has_term ( 'the-category-term-here', 'product_cat')) {
    $price = get_post_meta(get_the_ID(), '_regular_price', true);
    if( $price >= 1000 ){
        $monthlypayment = FinanceCalc();
        echo 'Finance this for $' . number_format($monthlypayment, 2) . ' per month';
    }
} ?>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your reply. I'm going to give this a shot. Just for the sake of my own understanding, can you further explain this line of code you wrote? $loanamount = $price >= 5000 ? $price + 99 : $price + 49; You've simplified the original if else statement I wrote. Is this just another way of writing if/else? – thisisbolo Dec 22 '17 at 17:48
  • Sorry, the code formatting isn't working in the comments. – thisisbolo Dec 22 '17 at 17:54
  • @thisisbolo yes this is the compact `if`/ `else` way… so you set in a variable, the condition before the `?` then come the `if` value and after `:` the `else` value… See: https://stackoverflow.com/questions/1506527/how-do-i-use-the-ternary-operator-in-php-as-a-shorthand-for-if-else – LoicTheAztec Dec 22 '17 at 17:56
  • That's amazing. Thank you for helping me learn @LoicTheAztec. I just tried this and it worked. I hope this is helpful for others seeking out something similar. If I wanted to take this one step forward, would I be able to use two if statements when calling this into my template file to limit this message showing if the price is only above $1000? – thisisbolo Dec 22 '17 at 18:01
  • Basically, how can I show/hide the output $monthlypayment = FinanceCalc(); if the price is under $1000. Right now, I show it based on a specific category term, can I add another 'if' to show/hide if price is under $1000? – thisisbolo Dec 22 '17 at 18:08
  • @thisisbolo Understood… I have maid an update at the end. Try it, it should work as you expect… – LoicTheAztec Dec 22 '17 at 18:14
  • 1
    That's awesome. This opened up tons of possibilities! Time to do some more learning/research. Much appreciated @LoicTheAztec. Happy holidays! – thisisbolo Dec 22 '17 at 18:17
  • @thisisbolo Thanks. Same thing to you :) … see you around. Bye. – LoicTheAztec Dec 22 '17 at 18:28