简体   繁体   English

将特定产品添加到购物车时如何删除特定的购物车项目?

[英]How to remove a specific cart item when adding to cart a specific product?

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.使用 WooCommerce,我想看看是否可以从购物车中删除特定商品(从购物车中),如果购物车中有另一个特定商品。

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.我尝试查看可能的选项和插件,但它们中的大多数都有基于定价之类的条件。

Yes is possible with a custom function hooked for example in woocommerce_add_to_cart hook:是的,例如在woocommerce_add_to_cart钩子中挂钩的自定义函数是可能的:

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' );
    }
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

This code is tested and works.此代码经过测试并有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 中为特定产品隐藏购物车中的“删除项目” - Hide “remove item” from cart for a specific product in WooCommerce 当来自特定产品类别的商品在 Woocommerce 的购物车中时禁用购物 - Disable shopping when an item from a specific product category is in cart in Woocommerce 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce WooCommerce:如果特定产品是购物车中的唯一商品,请清除购物车 - WooCommerce: If specific product is the only item in cart, clear cart 如何将购物车商品产品重定向到Woocommerce中的特定页面? - How can i redirect cart item product to specific page in Woocommerce? 如果购物车中有特定产品,请删除付款网关(Woocommerce) - Remove payment gateway if specific product is in the cart (Woocommerce) 从 WooCommerce 中的购物车中删除特定的产品变体 - Remove specific product variation from cart in WooCommerce Woocomerce 添加到购物车中的另一个特定项目时删除特定的购物车项目 - Woocomerce Remove a specific cart items when adding to cart another specific items 根据 WooCommerce 购物车总数添加或删除特定购物车项目 - Add or remove specific cart Item based on WooCommerce cart total 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM