Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active October 30, 2023 10:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/e4c6f841b08bad90f13a425cfe269720 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/e4c6f841b08bad90f13a425cfe269720 to your computer and use it in GitHub Desktop.
How to limit purchases to one per order
<?php
add_filter( 'woocommerce_add_to_cart_validation', 'wc_limit_one_per_order', 10, 2 );
function wc_limit_one_per_order( $passed_validation, $product_id ) {
if ( 31 !== $product_id ) {
return $passed_validation;
}
if ( WC()->cart->get_cart_contents_count() >= 1 ) {
wc_add_notice( __( 'This product cannot be purchased with other products. Please, empty your cart first and then add it again.', 'woocommerce' ), 'error' );
return false;
}
return $passed_validation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment