plugins – How do I call an action hook into wp_ajax hook callback function

Question

I have to trigger an ajax call based on a custom field changes on checkout form which is to add a discount. But if I call the hook to add discount into subtotal of the cart it does not do anything. Note that ajax call is working fine. Even if I use woocommerce_cart_calculate_fees hook outside of ajax callback function it worked.

add_action('wp_ajax_test', 'test');
add_action('wp_ajax_nopriv_test', 'test');

function test(){
  add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
}


function add_custom_fees( WC_Cart $cart ){

    // Calculate the amount to reduce
    $discount = $cart->subtotal * 0.2;
    $cart->add_fee( 'You have more than 3 items in your cart, a 10% discount has been added.', -$discount);
}

0
Fahad 1 month 2022-12-22T01:22:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse