简体   繁体   English

Woocommerce添加到购物车产品仅限4种特定产品

[英]Woocommerce Add to cart product Restriction to 4 specific products

I am using woocommerce and stuck in a situation. 我正在使用woocommerce并陷入困境。

I have 4 different products with IDs 1, 2, 3 and 4. 我有ID为1、2、3和4的4种不同产品。

What I need is that customer can add any 1 of the 4 items in cart at a time. 我需要的是,客户可以一次在购物车中添加4个商品中的任何一个。 For instance, if item 3 is in the cart then item 4 or 1 or 2 cannot be added to the cart. 例如,如果项目3在购物车中,则不能将项目4或1或2添加到购物车。

How do I get this in Woocommerce? 我如何在Woocommerce中获得这个?

Any help is appreciated. 任何帮助表示赞赏。

Updated: Try this, it should avoid, for defined product Ids, adding to cart when a another targeted product already exist in cart items, displaying a custom error message: 已更新:尝试此操作,对于已定义产品ID,应避免在购物车项目中已经存在另一个目标产品时将其添加到购物车中,并显示自定义错误消息:

add_filter( 'woocommerce_add_to_cart_validation', 'conditional_product_in_cart', 10, 3 );
function conditional_product_in_cart( $passed, $product_id, $quantity) {

    // HERE define your 4 specific product Ids
    $products_ids = array( 37, 40, 47, 53 );

    // Searching in cart for IDs
    if ( ! WC()->cart->is_empty() )
        foreach( WC()->cart->get_cart() as $cart_item ){
            $item_pid = $cart_item['product_id'];
            // If current product is from the targeted IDs and a another targeted product id in cart
            if( in_array( $item_pid, $products_ids ) && in_array( $product_id, $products_ids ) && $product_id != $item_pid ){
                $passed = false; // Avoid add to cart
                break; // Stop the loop
            }
        }

    if ( ! $passed ){
        // Displaying a custom message
        $message = __("You can only have one product in cart at the time.", "woocommerce");
        wc_add_notice( $message, 'error' );
    }
    return $passed;
}

Code goes in function.php file of the active child theme (or active theme). 代码进入活动子主题(或活动主题)的function.php文件中。

Tested and works 经过测试和工作

暂无
暂无

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

相关问题 "允许将 Woocommerce 中特定产品类别的最多 3 个产品添加到购物车" - Allow add to cart 3 products max for a specific product category in Woocommerce 在 WooCommerce 购物车页面的购物车商品名称后添加产品 ID 用于特定产品 - Add the product ID after the cart item name in WooCommerce cart page for specific products 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products Woocommerce 如果购物车中有特定产品,则将特定产品添加到购物车 - Woocommerce add to cart a specific product if a specific product is in the cart 允许根据 WooCommerce 中的购物车总数将特定产品添加到购物车 - Allow add to cart for specific products based on cart total in WooCommerce WooCommerce AJAX放入购物车以获取可变产品,仍重定向到产品页面 - WooCommerce AJAX Add to Cart for Variable products, still redirecting to product page 使用 Woocommerce 产品创建下拉菜单,并根据选择自动将产品添加到购物车 - Creating dropdown with Woocommerce products and automatically add product to cart based on selection 避免在WooCommerce中添加来自不同产品类别的购物车产品 - Avoid add to cart products from different product categories in WooCommerce 如果购物车包含特定的 Woocommerce 产品类别,则阻止添加到购物车 - Prevent add to cart if cart contains a specific Woocommerce product category 在WooCommerce中为特定产品添加额外添加到购物车按钮 - Add extra add to cart button to specific products in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM