php - 在 WooCommerce 4.5+ 中隐藏或编辑“您不能将另一个 'xxx' 添加到购物车”错误消息
问题描述
通过我启用的产品设置“单独销售:启用此功能以仅允许在单个订单中购买一件商品”
将相同的产品添加到购物车时,会出现一条错误消息,因为我启用了此设置。错误消息是“您不能将另一个 'xxx' 添加到您的购物车”。我不希望将相同的产品添加到购物车中,所以这很好用。
我的问题:
如何隐藏错误消息“您无法将另一个 'xxx' 添加到您的购物车”。
如果我使用 CSS 代码
.woocommerce-error {
display: none;
}
然后我们登录时的错误密码或电子邮件也被隐藏了,我不想隐藏另一个错误消息。
是否可以仅隐藏此错误?
解决方案
要编辑消息,您可以从WooCommerce 4.5.0开始使用woocommerce_cart_product_cannot_add_another_message
过滤器挂钩。
/**
* Filters message about more than 1 product being added to cart.
*
* @since 4.5.0
* @param string $message Message.
* @param WC_Product $product_data Product data.
*/
function filter_woocommerce_cart_product_cannot_add_another_message( $message, $product_data ) {
// New text
$message = __( 'My new message', 'woocommerce' );
return $message;
}
add_filter( 'woocommerce_cart_product_cannot_add_another_message', 'filter_woocommerce_cart_product_cannot_add_another_message', 10, 2 );
要完全隐藏消息,只需替换
// New text
$message = __( 'My new message', 'woocommerce' );
和
// New text
$message = '';
但是,上述解决方案的“问题”是消息现在被隐藏了,但woocommerce-error
(红色框)和view-cart
按钮仍然显示。
所以在使用过滤器钩子时,你可以添加一些额外的 jQuery 来隐藏woocommerce-error
注意:尽管以下方法有效,但隐藏错误消息绝不是一个好主意。这些是有原因的,让客户意识到一些事情。因此,这个解决方案有点“棘手”。
但要回答您的问题,您可以使用:
function filter_woocommerce_cart_product_cannot_add_another_message( $message, $product_data ) {
$message = '<div class="hide-this-error-message"></div>';
return $message;
}
add_filter( 'woocommerce_cart_product_cannot_add_another_message', 'filter_woocommerce_cart_product_cannot_add_another_message', 10, 2 );
function action_wp_footer() {
?>
<script>
jQuery(document).ready(function($) {
$( '.hide-this-error-message' ).closest( 'ul.woocommerce-error' ).hide();
});
</script>
<?php
}
add_action( 'wp_footer', 'action_wp_footer' );
推荐阅读
- java - 由于包 org.springframework.boot.autoconfigure.domain 不存在导致构建失败
- nginx - nginx:[emerg] 这里不允许使用“proxy_cache_path”指令,并且这里不允许使用“server”指令,并且
- android - 颤动运行错误和 gradle 失败,退出代码为 1
- php - 无法修改标头信息 - 从 /home/nyehfdcj/public_html/application/controllers/admin/Login.php:16 开始的输出已发送标头
- c++ - 引用另一个 const int 时删除 const int* const ptrs
- sql - 从单个列中的许多日期字符串中获取当前日期的最小值
- python - 如何通过 Python 中的装饰器检查参数和返回类型
- c# - 省略给定数字的第一个数字
- javascript - 使用 JS 发布时 php setcookie 不起作用
- visual-studio-code - FSharpLint,如何在 SuppressMessageAttribute 中使用规则“InterfaceNamesMustBeginWithI”?