4

I'm not sure I did this correctly after having read through the woocommerce_add_$NOTICE_TYPE hook. What I want to do is change the Coupon code applied successfully message into my own custom text like this:

The %coupon_name% promotion code has been applied and redeemed successfully.

But I don't know how to get the coupon name.

add_filter( 'woocommerce_add_notice', function( $message ){
// get the coupon name here
if( $message == 'Coupon code applied successfully.' )
$message = 'The %coupon_name% promotion code has been applied and redeemed successfully.';
return $message;
});
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

10

There is a specific hook for "success" notice when applying or removing a coupon code:

add_filter( 'woocommerce_coupon_message', 'filter_woocommerce_coupon_message', 10, 3 );
function filter_woocommerce_coupon_message( $msg, $msg_code, $coupon ) {
    // $applied_coupons = WC()->cart->get_applied_coupons(); // Get applied coupons

    if( $msg === __( 'Coupon code applied successfully.', 'woocommerce' ) ) {
        $msg = sprintf( 
            __( "The %s promotion code has been applied and redeemed successfully.", "woocommerce" ), 
            '<strong>' . $coupon->get_code() . '</strong>' 
        );
    }

    return $msg;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Third wow for today. I cannot thank you enough. I will put a donation in. Would you mind taking a look at my "empty session" question? If so, I will double the donation. Thanks again. https://stackoverflow.com/questions/62234187/automatically-empty-woocommerce-cart-and-delete-session-after-30-minutes –  Jun 06 '20 at 18:51