隐藏或编辑您无法在WooCommerce 4.5+中将另一个错误消息添加到购物车的错误消息(&Q;) [英] Hide or edit the "You cannot add another 'xxx' to your cart" error message in WooCommerce 4.5+

查看:34
本文介绍了隐藏或编辑您无法在WooCommerce 4.5+中将另一个错误消息添加到购物车的错误消息(&Q;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我启用的产品设置单独销售:启用此选项将仅允许在单个订单中购买一件商品&qot;

将相同的产品添加到购物车时,会出现一条错误消息,因为我启用了此设置。错误消息是";您无法将另一个‘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' );

这篇关于隐藏或编辑您无法在WooCommerce 4.5+中将另一个错误消息添加到购物车的错误消息(&Q;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆