久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

刪除帶有 $product_id 的項目 - Woocommerce

Remove item with $product_id - Woocommerce(刪除帶有 $product_id 的項目 - Woocommerce)
本文介紹了刪除帶有 $product_id 的項目 - Woocommerce的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

開發了一個功能,讓客戶在達到特定數量時將產品添加到購物車.

Made a function where the customer get a product added to the cart when they reach a specific amount.

客戶達到第 3 級并添加產品的示例.

// Bonus products
$product_1 = '4751'; 
$product_2 = '4752'; 
$product_3 = '4753'; 

// Get cart value in a clean format
$cart_total = WC()->cart->get_cart_subtotal();
$cart_total = html_entity_decode($cart_total, ENT_QUOTES, 'UTF-8');
$cart_total_format = strip_tags($cart_total);
$cart_value = preg_filter("/[^0-9]/", "", $cart_total_format);
$sum_raw = $cart_value;

// Set the sum level 
$level3 = '1500';

// Check sum and apply product
if ($sum_raw >= $level3) {

// Cycle through each product in the cart and check for match
$found = 'false';
foreach (WC()->cart->cart_contents as $item) {
    global $product;
    $product_id = $item['variation_id'];

    if ($product_id == $product_3) {
        $found = 'true';
    }
}

// If product found we do nothing 
if ($found == 'true') {}
// else we will add it
else {
    //We add the product
    WC()->cart->add_to_cart($product_3);

如果客戶決定刪除項目,那么此聲明是正確的,我希望能夠再次刪除它.

If customer decides to remove item's so this statement is true i want to be able to remove it again.

if ($sum_raw < $level3) {

    // Trying to remove item
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        if ($cart_item['variation_id'] == $product_3) {

            //remove single product
            WC()->cart->remove_cart_item($product_3);
        }
    }
}

我無法從購物車中移除產品.任何想法在這里做錯了什么?一直在四處尋找,但沒有找到任何適合我的解決方案.

Am do not manage to remove the product from cart. Any ideas what am doing wrong here? Have been searching around without finding any solution that works for me.

解決方案

在@Rohil_PHPBeginner & 的幫助下@WisdmLabs 我來到了這個為我完成工作的解決方案.

With help from @Rohil_PHPBeginner & @WisdmLabs I came to this solution that did the job for me.

global $woocommerce;
// Check if sum
if ($sum_raw < $level3) {
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {

        if ($cart_item['variation_id'] == $product_3) {
            //remove single product
            $woocommerce->cart->remove_cart_item($cart_item_key);
        }
    }
}

推薦答案

我認為您對 remove_cart_item 的使用不正確.如果您瀏覽文檔,您會發現它接受cart_item_key 作為參數(如評論中提到的 wisdmLabs).

I think you're using remove_cart_item incorrectly. If you go through the documentation, you will find that it accepts cart_item_key as parameter (as wisdmLabs mentioned in comment).

你是這樣使用它的:

WC()->cart->remove_cart_item($product_3);

試試這個:

WC()->cart->remove_cart_item($cart_item_key);

更新該行后,我認為您將能夠刪除產品.

After updating that line, I think you will able to remove product.

這篇關于刪除帶有 $product_id 的項目 - Woocommerce的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 国产精品久久久久久久久免费桃花 | 亚洲成人日韩 | 午夜寂寞影院在线观看 | 国产成人精品综合 | 亚洲激情在线观看 | 国产二区精品视频 | 亚洲成av人影片在线观看 | 99r在线| 日韩在线三级 | 日韩在线观看中文字幕 | 色橹橹欧美在线观看视频高清 | 久久国产高清视频 | 亚洲精品久久久一区二区三区 | 免费特级黄毛片 | 超碰97人人人人人蜜桃 | 成人小视频在线观看 | 狠狠的干狠狠的操 | 国产精品免费一区二区三区四区 | 精品欧美一区二区三区久久久 | 国产一区二区在线视频 | 欧美成人a | 99国产视频 | 精品无码久久久久久国产 | 99精品免费久久久久久日本 | 精品1区2区3区4区 | 一区二区三区四区不卡 | 久久久国产一区二区 | 亚洲国产精品人人爽夜夜爽 | 成人国产一区二区三区精品麻豆 | 中文字幕一区二区三区在线观看 | 亚洲永久入口 | 天天干国产 | 99视频在线播放 | 欧美精品片 | 亚洲成人精品国产 | 中文字幕一区二区三区四区五区 | 精品国产乱码久久久久久88av | 日韩日韩日韩日韩日韩日韩日韩 | 日韩免费在线观看视频 | 欧美日韩精品一区二区 | 久久99精品久久久久久 |