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.
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 ;
Happy coding.