替换 WooCommerce 中的 "Coupon code applied successfully." 消息

Replace "Coupon code applied successfully." Message in WooCommerce

我不确定在通读 woocommerce_add_$NOTICE_TYPE 钩子后我是否正确地执行了此操作。我想要做的是将 Coupon code applied successfully 消息更改为我自己的自定义文本,如下所示:

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

但是不知道优惠券名称怎么取

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;
});

应用或删除优惠券代码时,"success" 通知有一个特定的钩子:

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;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。