問題描述
在我的網站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.
一般:
刪除無法添加另一個...消息并在點擊后停止重定向到產品頁面.
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 錯誤您不能將另一個“產品名稱"添加到您的購物車的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!