【问题标题】:Customizing a WooCommerce notice with add_filter使用 add_filter 自定义 WooCommerce 通知
【发布时间】:2021-01-08 11:39:33
【问题描述】:

我正在尝试自定义 WooCommerce 通知。 这是我要替换的通知:

wc_add_notice( sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $_product->get_title() ), 'error' )

基于这个有用的答案WooCommerce Notice Messages, how do I edit them?,我想出了这个:

function my_woocommerce_membership_notice( $error ) {
    if ( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.' == $error ) {
        $error = '%s has been removed from your cart because you added a membership product. Please complete the membership purchase first.';
    }
    return $error;
}

add_filter( 'woocommerce_add_error', 'my_woocommerce_membership_notice' );

这会导致 HTTP500 错误,我无法弄清楚究竟是什么原因。

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce cart hook-woocommerce


    【解决方案1】:

    在互联网上搜索这个问题,似乎很多人在尝试使用类似的东西时遇到严重的类似错误问题......

    此错误消息设置在includes/class-wc-cart.php 的第 238 行。

    查看includes/wc-notice-functions.php 中的WC 2.6 版源代码,wc_add_notice() 正在处理2 个变量:$message$notice_type

    所以对于 $message 变量,我们有:
    sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $_product->get_title() )
    而不仅仅是:
    '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.'

    %ssprintf() 使用的字符串变量,将替换为 $_product->get_title() 值。但是你不能再在这里使用 %s 了。
    这可能是您的错误问题的原因。而不是'%s has been… 尝试'An item has been…

    然后基于this thread,使用strpos()条件里面的php函数,我编译了这个sn-p,不做任何保证:

    function my_woocommerce_membership_notice( $message ) {
        if (strpos($message,'has been removed from your cart because it can no longer be purchased') !== false) {
            $message = 'An item has been removed from your cart because you added a membership product. Please complete the membership purchase first.';
        }
        return $message;
    }
    add_filter( 'woocommerce_add_error', 'my_woocommerce_membership_notice' );
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2021-01-20
      相关资源
      最近更新 更多