You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce限制特定样品分类购物车最多添加3件商品

Solution to Limit "samples" Category Items to 3 in Cart

Hey there, let's fix that cart limit issue for your sample products! Since you've already set up your 0-price samples in the samples category with "Sold individually" enabled, we can use custom code to enforce the max 3 items rule for that entire category.

I’m assuming you’re using WooCommerce (given your setup details), so here’s a tested, practical snippet to make this work:

// Prevent adding more than 3 sample items to cart on initial add
add_filter('woocommerce_add_to_cart_validation', 'limit_samples_category_cart_items', 10, 3);
function limit_samples_category_cart_items($passed, $product_id, $quantity) {
    // Get the product's category slugs
    $product_cats = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
    
    // Only check for items in the "samples" category
    if (in_array('samples', $product_cats)) {
        $current_sample_count = 0;
        
        // Count existing sample items in the cart
        foreach (WC()->cart->get_cart() as $cart_item) {
            $item_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array('fields' => 'slugs'));
            if (in_array('samples', $item_cats)) {
                $current_sample_count++;
            }
        }
        
        // Block add-to-cart if total would exceed 3
        if ($current_sample_count + $quantity > 3) {
            wc_add_notice(__('You can only add up to 3 free samples to your cart.', 'woocommerce'), 'error');
            $passed = false;
        }
    }
    
    return $passed;
}

// Validate cart on update (blocks manual quantity changes or URL manipulation)
add_action('woocommerce_check_cart_items', 'limit_samples_category_cart_items_update');
function limit_samples_category_cart_items_update() {
    $sample_count = 0;
    $excess_items = array();
    
    // Count samples and flag excess items
    foreach (WC()->cart->get_cart() as $item_key => $cart_item) {
        $item_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array('fields' => 'slugs'));
        if (in_array('samples', $item_cats)) {
            $sample_count++;
            if ($sample_count > 3) {
                $excess_items[] = $item_key;
            }
        }
    }
    
    // Remove excess items and notify user
    if (!empty($excess_items)) {
        foreach ($excess_items as $key) {
            WC()->cart->remove_cart_item($key);
        }
        wc_add_notice(__('We’ve removed excess free samples from your cart — you can only have up to 3.', 'woocommerce'), 'error');
    }
}

How to implement this:

  • Option 1: Paste this code at the bottom of your active theme’s functions.php file (be sure to back up the file first to avoid typos breaking your site).
  • Option 2 (Recommended): Create a simple custom plugin to add this code. This way, your changes won’t get overwritten when you update your theme.

This code covers all bases:

  1. It stops users from adding more than 3 samples the moment they click "Add to Cart"
  2. It checks the cart every time it updates (like when quantities change) and automatically removes any excess samples
  3. It works seamlessly with your existing "Sold individually" setting, since we’re counting total unique sample items in the cart

内容的提问来源于stack exchange,提问作者sunweasel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:28:10