如何删除 woocommerce 错误 您无法将另一个“产品名称”添加到您的购物车 [重复]

2023-12-05

在我的网站 woocommerce 设置中,删除添加到卡 ajax 并注意;当用户(访客)将产品添加到购物篮进行购买时,点击购物篮后重定向并显示消息将产品添加到购物篮中的卡片成功

但是当产品选项处于活动状态(启用)时,我想单独出售选项。 用户尝试反复将产品添加到购物车。收到以下消息: 无法将另一个“产品名称”添加到您的购物车。 我的问题是如何使用functions.php删除此woocommerce错误您无法将另一个“产品名称”添加到您的购物车。

重复单击购物篮中的“添加到购物车”按钮后,新消息会显示在购物篮中 您之前将“产品名称”添加到您的购物车。所以现在你可以付款了。

一般来说:

  1. 删除无法添加另一条...消息并在单击后停止重定向到产品页面。

  2. 显示新的自定义消息。点击后进入购物篮。

非常感谢大家


这是一个经过测试且有效的解决方案,用于删除“您无法添加另一个”消息。

背景:Woocommerce 不会公开其所有通知的直接挂钩。购物车错误实际上被硬编码到 class-wc-cart.php 中作为抛出的异常。

当生成错误异常时,它们会被添加到我们可以使用以下方法访问、解析和更改的通知列表中:

  • wc_get_notices()以数组形式返回所有通知
  • wc_set_notices()让您直接设置通知数组

为了访问通知并更改它们,您需要挂钩一个操作,该操作将在 woocommerce 生成通知后但在显示页面之前触发。您可以通过以下操作来做到这一点:woocommerce_before_template_part

这是完整的工作代码,专门删除了“您无法添加另一个” 通知:

add_action('woocommerce_before_template_part', 'houx_filter_wc_notices');

function houx_filter_wc_notices(){
        $noticeCollections = wc_get_notices();

        /*DEBUGGING: Uncomment the following line to see a dump of all notices that woocommerce has generated for this page */
        /*var_dump($noticeCollections);*/

        /* noticeCollections is an array indexed by notice types.  Possible types are: error, success, notice */
        /* Each element contains a subarray of notices for the given type */
        foreach($noticeCollections as $noticetype => $notices)
        {
                if($noticetype == 'error')
                {
                        /* the following line removes all errors that contain 'You cannot add another'*/
                        /* if you want to filter additiona errors, just copy the line and change the text */
                        $filteredErrorNotices = array_filter($notices, function ($var) { return (stripos($var, 'You cannot add another') === false); });
                        $noticeCollections['error'] = $filteredErrorNotices;
                }
        }

        /*DEBUGGING: Uncomment to see the filtered notices collection */
        /*echo "<p>Filtered Notices:</p>";
        var_dump($noticeCollections);*/

        /*This line overrides woocommerce notices by changing them to our filtered set. */
        wc_set_notices($noticeCollections);
}

旁注:如果您想添加自己的通知,可以使用 wc_add_notice()。您必须阅读 woocommerce 文档才能了解其工作原理:WooCommerce 文档上的 wc_add_notice

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何删除 woocommerce 错误 您无法将另一个“产品名称”添加到您的购物车 [重复] 的相关文章