如何删除 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

相关文章