4

In Woocommerce, is there any way trigger JS after applied coupon like this?

I would like to use it in something like this:

    $( document.body ).on( 'updated_cart_totals', function(){
        //do something      
    });

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

4

You can use applied_coupon delegated event like in this example that will display an alert box with the applied coupon code (where the variable coupon is the applied coupon code):

<script type="text/javascript">
jQuery(function($){
    $( "body" ).on( 'applied_coupon', function(event, coupon){
        // Example: Displaying an alert message with the applied coupon code
        alert('Applied coupon code: "' + coupon + '"\n' + 'Event type: "' + event.type + '"');
    });
});
</script>

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 3
    +1 for @LoicTheAztec's answer. Just for those who would like to use this at the checkout, the trigger is applied_coupon_in_checkout. – bonobo Aug 19 '21 at 17:34