All snippets

WooCommerce samples: one unit per product in the cart

Apply WooCommerce’s sold-individually rule to a sample category. Covers variation behaviour, cart checks and the limits of the restriction.

Sample products can be limited to one unit of a particular product in the standard cart. For a few items, enable Sold individually in their product settings. This code applies the same rule to products directly assigned to the category slug vzorky.

It does not impose a lifetime customer limit or a single sample across the entire catalogue. Two different sample products can coexist in the cart, one unit each.

PHP code

Check the category slug under Products → Categories and replace vzorky if yours differs. On a test site, add the example to a PHP snippet set to run everywhere or to your child theme's functions.php. The opening <?php tag is omitted; enable one copy only.

add_filter( 'woocommerce_is_sold_individually', function ( $sold_individually, $product ) {
    if ( $sold_individually || ! $product instanceof WC_Product ) {
        return $sold_individually;
    }
    if ( ! $product->is_type( array( 'simple', 'variable', 'variation' ) ) ) {
        return $sold_individually;
    }

    $product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
    if ( $product_id < 1 ) {
        return $sold_individually;
    }

    return has_term( 'vzorky', 'product_cat', $product_id );
}, 20, 2 );

How the rule behaves

An existing sold-individually setting remains enabled even outside the sample category. Variations use their parent's category. In a normal cart, each variation is a separate combination: one blue and one red variation can therefore coexist. The code does not impose a shared limit across all variations of a parent.

Assignment to a child category does not automatically count as direct assignment to the vzorky parent category. Assign the product to the specified category or adapt the rule. The code changes neither prices nor stock and does not make an unavailable item purchasable.

What to test

Add a sample with a requested quantity of two. WooCommerce's native add-to-cart flow reduces that quantity to one. Adding the same item again should be rejected. Also test an ordinary product outside the category, an item already sold individually and two variations. For an existing cart containing a higher quantity, check validation before payment.

Extensions that distinguish cart items using custom data may affect item identity. Do not describe this rule as protection against repeat purchases. Administrator or API order creation outside the cart is not generally blocked by this snippet.

Removal and related settings

Disabling the snippet removes the category rule while preserving manually enabled product settings. On multilingual sites, check the slug of each translated category. Server checks passed with WordPress 7.1.5, WooCommerce 11.3.1 and PHP 8.4.23; verify your own classic or block checkout separately.

For products sold in larger quantities, consider quantity discounts. A sample limited to one unit cannot itself reach a five-unit tier. Native cart validation source