<?php 
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
add_filter( 'woocommerce_add_to_cart_validation', 'kanthemes_only_one_in_cart', 99, 2 );
function kanthemes_only_one_in_cart( $passed, $added_product_id ) {
   wc_empty_cart();
   return $passed;
}
