Search code examples
phpwordpresswoocommerceproduct

Loan Calculator to Show Monthly Payment Amount on Woocommerce Content Product Loop


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.


Solution

  • 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';
        }
    } ?>