Limit Cart to a Single Item
Prevents customers from adding more than one item to the cart at a time, clearing the existing item when a new product is added.
PHPby SnipCraft
php
<?php
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
add_filter( 'woocommerce_add_to_cart_validation', 'scseed_limit_cart_one_item', 10, 2 );
function scseed_limit_cart_one_item( $passed, $product_id ) {
if ( WC()->cart->is_empty() ) {
return $passed;
}
wc_add_notice(
__( 'Only one item is allowed in the cart at a time. The existing item has been replaced.', 'woocommerce' ),
'notice'
);
WC()->cart->empty_cart();
return $passed;
}#cart#checkout#single-item#ux#woocommerce