如何清除woocommerce错误您不能在购物车中添加另一个“产品名称" [英] How to remove woocommerce error You cannot add another “PRODUCT NAME” to your cart

查看:74
本文介绍了如何清除woocommerce错误您不能在购物车中添加另一个“产品名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in my website woocommerce settings, remove add to card ajax and notice; and when users (visitors) add a product to basket for buy, redirect after click to basket and show message add product to card successful in basket

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

but when in product option active (enable) I want to sell alone option. users try for add product to basket for repeatedly. receive below message: cannot add another "PRODUCT NAME" to your cart. my question is How to use functions.php for remove this woocommerce error You cannot add another "PRODUCT NAME" to your cart.

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

and new message show in basket after repeat click add to cart button in basket you previously "PRODUCT NAME" to your cart. so now you can pay.

通常:

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

  1. remove cant add another ... message and stop redirect to product page after click.

显示新的自定义消息.单击并转到购物篮后.

show new custom message. after click and go to basket.

非常感谢大家

推荐答案

这是一种经过测试且有效的解决方案,用于删除您无法添加其他"消息.

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

Background: Woocommerce does not expose direct hooks to all of its notices. The cart errors are actually hardcoded into class-wc-cart.php as thrown Exceptions.

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

When Error Exceptions are generated, they get added to a list of Notices that we can access, parse, and alter using the methods:

  • wc_get_notices()将所有通知作为数组返回
  • wc_set_notices()可让您直接设置通知数组
  • wc_get_notices() returns all notices as an array
  • wc_set_notices() lets you set the notices array directly

为了访问通知并对其进行更改,您需要挂钩一个在woocommerce生成其通知后将触发的操作,但是在显示该页面之前.您可以执行以下操作: woocommerce_before_template_part

In order to access the notices and alter them, you need to hook an action that will fire after woocommerce has generated its notices, but BEFORE the page is displayed. You can do that with the action: woocommerce_before_template_part

这里是完整的工作代码,专门删除了"您不能添加其他"通知:

Here is complete working code that specifically removes "You cannot add another" notices:

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

Side note: If you wanted to add your own notice, you can use wc_add_notice(). You'll have to read the woocommerce documentation to see how it works: wc_add_notice on WooCommerce docs

这篇关于如何清除woocommerce错误您不能在购物车中添加另一个“产品名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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