【问题标题】:Replace "Coupon code applied successfully." Message in WooCommerce替换“优惠券代码申请成功”。 WooCommerce 中的消息
【发布时间】:2020-09-25 20:20:54
【问题描述】:

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

【问题讨论】:

    标签: php wordpress woocommerce cart coupon


    【解决方案1】:

    在应用或删除优惠券代码时,有一个特定的“成功”通知挂钩:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 今天的第三个惊喜。我不知道该怎么感谢你才足够。我会捐款。你介意看看我的“空会话”问题吗?如果是这样,我将加倍捐款。再次感谢。 stackoverflow.com/questions/62234187/…
    猜你喜欢
    • 2014-11-26
    • 2018-05-11
    • 2017-09-24
    • 2017-11-07
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2017-07-13
    • 2017-08-09
    相关资源
    最近更新 更多