how to show calculated value in backend on woocommerce checkout page

217 Views Asked by At

I'm creating an online store for my company which is a subsidiary of a bank. With the help of banking loan we want to build the infrastructure of selling goods via installment. By using this plugin I managed to add a select field which let the buyer to select his loan plan. there are other fields but not important here. I also added update_totals_on_change class to this field so when it changes whole check out page refreshes via ajax. I managed to calculate amount of installment in woocommerce_cart_calculate_fees each time the loan plan changes in this way :

add_action( 'woocommerce_cart_calculate_fees', 'manipulate_order', 99 );

function manipulate_order( $cart ) {

    if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
         return;
    }
    $post_data=null;
    if ( isset( $_POST['post_data'] ) ) {
        parse_str( $_POST['post_data'], $post_data );
    } else {
        $post_data = $_POST; 
    }

    session_start();
    $_SESSION['payment'] = $post_data['payment'];

    if($post_data['payment'] == 'cash')
    {
        WC()->cart->fees_api()->set_fees(array());
    }
    else
    {
        $items = WC()->cart->get_cart_contents();
        $cartInfo = array();
        foreach ($items as $item => $values) {
            $productInfo = array();
            array_push($productInfo, $values['data']->get_id());
            array_push($productInfo, $values['quantity']);
            array_push($productInfo, get_post_meta($values['product_id'], '_price', true));
            array_push($productInfo, get_post_meta($values['product_id'], 'interest', true));
            array_push($cartInfo, $productInfo);
        }
        $targetBenefit = 7;
        $total_price = 0;
        $average_weighted_interest = 0;
        for ($i = 0; $i < count($cartInfo); $i++) {
             $total_price += ($cartInfo[$i][1] * $cartInfo[$i][2]);
            $average_weighted_interest += $cartInfo[$i][1] * $cartInfo[$i][2] * $cartInfo[$i][3] / 100;
        }
         
        $average_weighted_interest = round(100 * $average_weighted_interest / $total_price,2);
        $prepayment_percent = $post_data['prepayment'];
        $bank_wage_percent = explode("_",$post_data['loan'])[0];
        $bank_wage_period = explode("_",$post_data['loan'])[1];
        $adjusted_bank_wage_percent = round($bank_wage_percent*(1-$prepayment_percent/100),2);
        $increase_percent = round($targetBenefit + $adjusted_bank_wage_percent - $average_weighted_interest,2);
        $added_expense = round($total_price * $increase_percent/100,2);
        $loan_amount = round((($total_price + $added_expense)-$total_price*$prepayment_percent/100),2);
        $_SESSION['loan_amount'] = $loan_amount;
        $installment =  calPMT(18, $bank_wage_period, $loan_amount);
        $_SESSION['installment'] = $installment;
        
        WC()->cart->add_fee('Added Expense: ', $added_expense);
        WC()->cart->add_fee('Loan Amount: ', 0-$loan_amount);
        if($loan_amount > 150000){
            wc_add_notice( __( 'Your Loan amount is bigger than maximum, delete some product or increase prepayment' ), 'error' );
        }
    }
}

function calPMT($apr, $term, $loan)
{
    $term = $term;
    $apr = $apr / 1200;
    $amount = $apr * -$loan * pow((1 + $apr), $term) / (1 - pow((1 + $apr), $term));
    return round($amount);
}

I also added Two more text read-only fields to the checkout, One for the loan_amount with the same id and the other for installment also with the same id. I want to return these two calculated values from the menstioned action to the front end and there to show them as the value of the two read-only fields each time that the whole checkout page updates. there are bunch of useful events here .probably update_checkout event suits my problem best but I have no idea how to return these two values from manipulate_order callback to front end and there to show in read-only fields .

0

There are 0 best solutions below