<?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).