// Solo muestra el último producto añadido y elimina los anteriores en caso de que hubiese
add_action( 'woocommerce_before_cart_contents', 'allow_only_the_last_product_added' );
add_action( 'woocommerce_before_checkout_form', 'allow_only_the_last_product_added' );
function allow_only_the_last_product_added() {
$lastone_key = null;
// Remove all products except the last one
if ( WC()->cart->get_cart_contents_count() > 1 ) {
// Get the key of the last product added
$cart_content = WC()->cart->get_cart();
$lastone_key = key( array_slice( $cart_content, -1, 1, TRUE ) );
foreach ( $cart_content as $key => $product ) {
if( $key != $lastone_key ){
WC()->cart->remove_cart_item( $key );
}
}
}
}