Last active
October 30, 2023 10:07
-
-
Save SirDarcanos/e4c6f841b08bad90f13a425cfe269720 to your computer and use it in GitHub Desktop.
How to limit purchases to one per order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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