I need to check if a customer has purchased a specific product earlier in WooCommerce.
The case is this: The customer shall not be able to purchase product "c", "d", "e" unless they have purchased product "a" or "b" at an earlier time.
If the customer has purchased product "a" or "b" earlier, then the purchase button of product "c", "d" and "e" is activated and they are allowed to buy them.
If they haven´t purchased "a" or "b" earlier, they will not be allowed to purchase "c", "d", "e" and the purchase button is deactivated.
How can I achieve this?
Thanks.
I'd be doing it this way;
this won't be doing any woocommerce template modification but only use the filter
woocommerce_is_purchasable
These are all the functions,
check if current product ID is already brought by customer, in version 2.6 +, woocommerce implemented the function
wc_customer_bought_product
to check if the customer already brought the product, I've never used it before but based on the docs you can create a function like below to check an array of product ID if one of them has been already brought;The previous method I do to check if specific product ID has already been brought by the customer is below, so you can use any of these function, though the one I created is not for array of ID's
woocommerce_is_purchasable
to create our own condition, this function below is a simply example of what you are trying to achieve,just changed the
$required_purchased
and$conditional_purchase
values.And that should set the product as non-purchasable (add to cart button will be automatically hidden ). Now if you want to add a message for non-purchasable product, you can simply use the same condition of
cmk_disable_product_purchase
, add your message and simply hook it onwoocommerce_single_product_summary
or anywhere you want it to display.The built-in woocommerce function
wc_customer_bought_product
can also be used in this case.See function usage here.
Find below a new version of my conditional function partially based on the built-in woocommerce function
wc_customer_bought_product
source code:There is an 2 optional argument
$user_id
and$product_ids
:$user_id
will allow you to specify a defined user ID (when is not used for current logged in user);$product_ids
(array) will allow to specify defined product Ids to checkHere is that code:
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on Woocommerce 3+ and works (It should work on previous versions too).
USAGE EXAMPLES:
Example 1 (logged in customer): Detecting if current user has bought one of the defined products (product Ids needs to be an array)
Example 2 (setting the
$user_id
) Detecting if the defined user has bought one of the defined products (product Ids needs to be an array)If the
$user_id
is not defined and the current user is not logged in, this function will returnfalse
.Example 3 (logged in customer): Detecting if current user has already made a purchase
Example 4 (setting the
$user_id
) Detecting if the defined user has already made a purchaseYes it's possible, writing a conditional function that returns "true" if current customer has already bought specifics defined products IDs. This code goes on function.php file of your active child theme or theme.
Here is the conditional function:
This code is tested and works.
USAGE:
For example, you can use it in some WooCommerce templates that you will have previously copied to your active child theme or theme:
add-to-cart
button isloop/add-to-cart.php
.add-to-cart
button are insingle-product/add-to-cart
folder depending on your product types.Here is an example that you could use in those templates (above):
And here the complete applied example to
add-to-cart
button template on Shop page:You will style the inactive button with
greyed_button
class in thestyle.css
file of your active child theme or theme. Same thing for the message withgreyed_button-message
class.