Woocommerce only one product in cart

Only one product purchase at a time by use in wordpress wocommerece can be possible adding the filter in the theme functions.php file this is a cutom function so you can add in your child theme functions.php file anywhere if you have not the child theme you can add this code the the functions.php file of the regular theme. The logic to do this we just use a custom function in which we empty cart and then we add our product in the cart that i want to purchase and ad we call this function on the add to cart filter. Here is the code you can use by simply copying in your theme functions file.


/// ONLY ONE PRODUCT IN CAR AT A TIME
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );


function woo_custom_add_to_cart( $cart_item_data ) {
    
    global $woocommerce;
    // Empty cart remove all the items already in the cart.

    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

Students Tech Life