Php 使用添加过滤器自定义WooCommerce通知
我正在尝试定制一个WooCommerce通知。 这是我试图替换的通知:Php 使用添加过滤器自定义WooCommerce通知,php,wordpress,woocommerce,cart,hook-woocommerce,Php,Wordpress,Woocommerce,Cart,Hook Woocommerce,我正在尝试定制一个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' ) 基于这个有用的答案,
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' )
基于这个有用的答案,我得出了以下结论:
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出现错误,我不知道确切的原因
谢谢 在互联网上搜索这个问题,似乎很多人在尝试使用类似的东西时都有严重的类似错误问题 此错误消息设置在第238行 查看中的WC 2.6版源代码,
WC\u add\u notice()
正在处理两个变量:$message
和$notice\u type
因此,对于$message变量,我们有:sprintf(“%s”已从您的购物车中删除,因为它无法再购买。如果您需要帮助,请与我们联系。“,”woocommerce“,$\u产品->获取标题())
而不仅仅是:
%s已从您的购物车中删除,因为它无法再购买。如果您需要帮助,请与我们联系。“
%s
是sprintf()
使用的字符串变量,将替换为$\u product->get\u title()
值。但是你不能再在这里使用%s
这可能是您出现错误问题的原因之一。请尝试
'一个项目已被…
,而不是。
然后基于,在条件中使用strpos()
php函数,我编译了这个片段,没有任何保证:
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' );