What can I add to the functions.php file of my theme that would make it so that users could only buy a product once? As in if they have ever bought any product in the store before, they are prevented from purchasing it again.
I imagine the code would look something like this (pseudo code) and would use the woocommerce_add_cart_item_data filter.
add_filter( 'woocommerce_add_cart_item_data', 'woo_check_not_bought_before' );
function woo_check_not_bought_before( $cart_item_data ) {
global $woocommerce;
// Some MYSQL to check if product was bought before
if ($bought_before == 'true') {
$woocommerce->cart->empty_cart();
}
// Echo some text to explain why you can only buy the product once
return $cart_item_data;
}
here’s a simple solution.
screenshot
problem with this though is that anyone can create a new user and buy again.
You might use this code to achieve your requirement. Create a folder called woocommerce in your theme. Then create a folder named loop in it. Then create a add-to-cart.php file in it and write the below code:
But the user can still make a different account and buy that product again.
Adding onto WisdmLabs’s answer, you will also need to edit the shortcodes and other instances of the add to cart button.
To do this, create another folder:
/wp-content/themes/[theme]/woocommerce/single-product/add-to-cart/
Create a file in that folder: simple.php and add the following code:
You will then also be able to edit the “Purchased” text using css class: product-purchased.
Separate files needs to be added with different code for external.php, grouped.php, variable.php if you want this functionality to work for those product types as well.