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

將特定產品添加到購物車時如何刪除特定的購物

How to remove a specific cart item when adding to cart a specific product?(將特定產品添加到購物車時如何刪除特定的購物車項目?)
本文介紹了將特定產品添加到購物車時如何刪除特定的購物車項目?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

使用 WooCommerce,我想看看是否可以從購物車中刪除特定商品(如果購物車中有另一個特定商品).

With WooCommerce, I am looking to see if it's possible to remove a specific item (from cart), if another specific item is in the cart.

我的網上商店有一個產品的免費版本,讓客戶可以基本訪問網站內容.付費版本將開放更多內容訪問權限.這樣,如果免費版本已經在購物車中,并且付費版本被添加到購物車中,那么免費版本將從購物車中刪除.

My web shop has a free version of a product which gives to customer a basic access to site content. The paid version will open up more access to content. This way if the free version is already in the cart, and the paid version gets added to cart, then the free version will be removed from cart.

我嘗試查看可能的選項和插件,但其中大多數都有基于定價和類似內容的條件.

I tried looking at possible options and plugins but most of them have conditions based on pricing and things like that.

推薦答案

是的,例如在 woocommerce_add_to_cart 鉤子中掛鉤的自定義函數是可能的:

Yes is possible with a custom function hooked for example in woocommerce_add_to_cart hook:

add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {

    // Set HERE your targeted product ID
    $target_product_id = 31;
    // Set HERE the  product ID to remove
    $item_id_to_remove = 37;

    // Initialising some variables
    $has_item = false;
    $is_product_id = false;

    foreach( WC()->cart->get_cart() as $key => $item ){
        // Check if the item to remove is in cart
        if( $item['product_id'] == $item_id_to_remove ){
            $has_item = true;
            $key_to_remove = $key;
        }

        // Check if we add to cart the targeted product ID
        if( $product_id == $target_product_id ){
            $is_product_id = true;
        }
    }

    if( $has_item && $is_product_id ){
        WC()->cart->remove_cart_item($key_to_remove);

        // Optionaly displaying a notice for the removed item:
        wc_add_notice( __( 'The product "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
    }
}

此代碼位于活動子主題(或主題)的 function.php 文件或任何插件文件中.

此代碼已經過測試且有效.

This code is tested and works.

這篇關于將特定產品添加到購物車時如何刪除特定的購物車項目?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 亚洲精品专区 | 午夜小视频在线播放 | 午夜激情小视频 | 国产视频中文字幕 | 逼逼网 | 91网站在线看 | 欧美极品在线播放 | 久久久www成人免费精品张筱雨 | 色婷婷综合久久久久中文一区二区 | 黄色一级大片在线免费看产 | 精品视频在线免费观看 | 精品久久国产 | 亚洲天堂中文字幕 | 久久久久免费精品国产小说色大师 | 亚洲一区二区久久久 | 国产精品亚洲片在线播放 | 国产露脸国语对白在线 | 精品久久久久久久久久久久久 | 日韩在线国产精品 | 国产精品久久在线观看 | 久久精品a | 国产成人午夜精品影院游乐网 | 国产精品一区二区在线观看 | 手机av在线| 久久网日本 | 成人免费在线播放 | 国产精品欧美一区二区 | 在线黄色网 | 国产精品视频网址 | 玖草资源| 亚洲精品99| 日本网站免费在线观看 | 欧美一区二区三区久久精品 | 日韩精品 电影一区 亚洲 | 91看片免费 | 国产一区二区在线播放视频 | 久久曰视频 | www.亚洲国产精品 | 人人鲁人人莫人人爱精品 | 精品美女视频在免费观看 | 日韩三级在线观看 |