14.9 C
London
Wednesday, April 15, 2026

wp_ajax Hook Not Working with WooCommerce Fees (Simple Fix)

- Advertisement -spot_imgspot_img
- Advertisement -spot_imgspot_img

Introduction

This issue comes when using AJAX with WooCommerce.

You trigger AJAX call. It works fine.

But when you try to add discount using hook inside AJAX, nothing happens.

This is confusing because same hook works outside AJAX.


Problem

You have code like this:

add_action('wp_ajax_test', 'test');
add_action('wp_ajax_nopriv_test', 'test');function test(){
  add_action('woocommerce_cart_calculate_fees', 'add_custom_fees');

And discount function:

function add_custom_fees($cart){
  $discount = $cart->subtotal * 0.2;
  $cart->add_fee('Discount', -$discount);
}

AJAX runs. But discount not applied.


Why It Not Working

This is main point.

AJAX request is separate request.

When you call add_action inside AJAX:

  • It works only for that AJAX request
  • It does not stay for next request

But WooCommerce cart calculation happens later.

So hook is not there when needed.


Simple Understanding

Think like this:

  • AJAX = one time run
  • Checkout submit = different run

Your hook is not saved between them.


Correct Approach

Do not add WooCommerce hook inside AJAX.

Use AJAX only to check condition.

Then apply discount in normal WooCommerce hook.


Step 1: Use AJAX for Checking

add_action('wp_ajax_discount', 'check_discount');function check_discount(){
    $apply = true; // your logic    if($apply){
        echo json_encode(['status' => 'yes']);
    } else {
        echo json_encode(['status' => 'no']);
    }
    die();
}

Step 2: Apply Discount Properly

add_action('woocommerce_cart_calculate_fees', 'add_my_discount');function add_my_discount($cart){    if( !isset($_POST['apply_discount']) ) return;    $discount = $cart->subtotal * 0.2;    $cart->add_fee('Discount applied', -$discount);
}

How It Works

  • AJAX checks if discount is valid
  • Frontend stores result (hidden field or JS)
  • On checkout submit โ†’ WooCommerce hook runs
  • Discount applied correctly

Important Points

  • AJAX is not permanent
  • Hooks inside AJAX do not persist
  • WooCommerce fees must run in main request

Small Example

One case:

User selects option on checkout.

AJAX checks condition โ†’ returns โ€œdiscount allowedโ€

Frontend adds hidden field.

On checkout submit โ†’ discount applied using hook.


Common Mistakes

  • Adding hook inside AJAX
  • Expecting AJAX to store state
  • Not passing data to checkout
  • Running fee logic in wrong place

Quick Tip

If discount depends on cart:

Always use:

woocommerce_cart_calculate_fees

Not AJAX callback.


FAQs

Why hook inside AJAX not working?

Because AJAX is separate request.

Can AJAX apply discount directly?

No, not permanently.

Where to apply discount?

In WooCommerce hooks.

What is best hook?

woocommerce_cart_calculate_fees

Do I need frontend change?

Yes, to pass data from AJAX


Final

Main issue is misunderstanding AJAX flow.

AJAX is only for checking or UI.
Real logic should run in WooCommerce hooks.

Keep it simple

- Advertisement -spot_imgspot_img
Latest news
- Advertisement -spot_img
Related news
- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here