0

I would like to limited users from adding a second product at the cart. But he has the ability to add more variations of the first product. I he add a t-shirt with variation red he can only add t-shirt with bluw varation an he can't add trousers.

0

You can hook into woocommerce_before_calculate_totals and do a check for cart items, if there are more than one, remove all previous cart items and add the last one added.

add_action( 'woocommerce_before_calculate_totals', 'wcaopc_keep_only_last_cart_item', 30);
function wcaopc_keep_only_last_cart_item ($cart) {
    if (is_admin() && ! defined('DOING_AJAX')) return;
    if (did_action('woocommerce_before_calculate_totals') >= 2) return;
    
    $cart_items = $cart->get_cart();
    
    if (count($cart_items) > 1){
        $cart_item_keys = array_keys($cart_items);
        $cart->remove_cart_item(reset($cart_item_keys));
    }
}

credit to Download Allow only 1 product in Cart

Not the answer you're looking for? Browse other questions tagged or ask your own question.