WordPress WooCommerce

WooCommerce Allow Only One Product In Cart

19 October 2021

Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 11 in /srv/users/prosvit/apps/prosvit/public/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 13 in /srv/users/prosvit/apps/prosvit/public/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 14 in /srv/users/prosvit/apps/prosvit/public/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 31 in /srv/users/prosvit/apps/prosvit/public/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 33 in /srv/users/prosvit/apps/prosvit/public/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 34 in /srv/users/prosvit/apps/prosvit/public/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466
<?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 *

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert