Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch "applied coupon" event to trigger a JS function in Woocommerce

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.

like image 810
Akhtarujjaman Shuvo Avatar asked Sep 14 '25 04:09

Akhtarujjaman Shuvo


1 Answers

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.

like image 193
LoicTheAztec Avatar answered Sep 16 '25 20:09

LoicTheAztec