WordPress WooCommerce

WooCommerce Allow Only One Product In Cart

19 October 2021
<?php
/**
* @snippet WooCommerce - allow only one product in cart
* @author Prosvit Design Team
* @website https://prosvit.design
*/
add_filter( 'woocommerce_add_to_cart_validation', 'prosvit_woocommerce_add_to_cart_validation', 99, 2 );
function prosvit_woocommerce_add_to_cart_validation( $passed, $added_product_id ) {
// Products ids that will trigger this behaviour
$products = array(1, 2, 3);
if ( in_array( $added_product_id, $products ) ) {
wc_empty_cart();
}
return $passed;
}

Does this snippet (still) work?

Please let us know in the comments if everything worked as expected. We would be happy to revise the snippet if you report otherwise (please provide screenshots).

Leave a Reply

Your email address will not be published. Required fields are marked *

Array

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert