Search code examples
woocommercehook-woocommercewoocommerce-rest-apihook-wordpress

Updating cart totals after ajax request


Here is my custom ajax request callback.

I use some data with wp_remote_post and getting json results about my own woocommerce payment gateway installments.

/**
 * Check avaliblity for installments via WordPress Ajax
 *
 * @return void
 */
function check_installment() {

    if ( isset($_REQUEST) ) {

        $action = data_get($_REQUEST, 'action');
        if($action == 'check_installment')
        {

            $cart_data = WC()->session->get('cart_totals');  
            $binNumber = data_get($_REQUEST, 'bin');

            if(!$binNumber)
            {
                return;
            }
            
            $_initGateway = new Woo_Ipara_Gateway();
            $_initGateway = $_initGateway->checkInstallment($binNumber);
            $data = [
                'cardFamilyName' => data_get($_initGateway, 'cardFamilyName'),
                'supportedInstallments' => data_get($_initGateway, 'supportedInstallments')
            ];
            echo json_encode(getInstallmentComissions(data_get($_initGateway, 'cardFamilyName')));  
            
        }
    
    } 
    die();
}
add_action( 'wp_ajax_check_installment', 'check_installment');
add_action( 'wp_ajax_nopriv_check_installment', 'check_installment');

Right now, payment provider, have different comissions for spesific credit card. So it means, i want to change order total after this request, when user's selected installment value.

I also found some filter, about calculated total woocommerce_calculated_total, but how to trigger this, after ajax request and user, selected installment choice?


add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function custom_calculated_total( $total, $cart ){
    // some magic.
}

Any helps ? Thanks.


Solution

  • First of all, filter method was wrong, what i wanted to try woocommerce_calculated_total.

    It must be, woocommerce_checkout_order_processed

    Other issue is, WC()->cart->add_fee( "text", $fee, false, '' ); not really work correctly.

    You should use, new WC_Order_Item_Fee() class in your action directly.

    Here is the some part of code in my case ;

    add_action('woocommerce_checkout_order_processed', 'addFeesBeforePayment', 10, 3);
    function addFeesBeforePayment( $order_id, $posted_data, $order ) {
    
        // some logic about bin verification and remote fetch about installment comissions. 
         
        $cartTotal = WC()->cart->cart_contents_total;
     
        $newCartTotal = $cartTotal + ( $cartTotal * $defaultComission / 100 ); 
        $installmentFee = $cartTotal * ($defaultComission / 100 );   
    
        $item_fee = new WC_Order_Item_Fee();
    
        $item_fee->set_name( 'Kredi Kartı Komisyonu ('.$defaultInstallment.' Taksit)' ); // Generic fee name
        $item_fee->set_amount( $installmentFee ); // Fee amount
        $item_fee->set_tax_class( '' ); // default for ''
        $item_fee->set_tax_status( 'none' ); // or 'none'
        $item_fee->set_total( $installmentFee ); // Fee amount 
        $order->add_item( $item_fee ); 
        $order->calculate_totals(); 
    
        $order->add_order_note( 'Bu sipariş için ödeme '. $defaultInstallment . ' taksit seçeneği seçilerek oluşturuldu. Toplam taksit komisyonu, sipariş tutarına ek olarak ' . $installmentFee. ' TL karşılığında eklendi.' );
    }
    

    Happy coding.