WooCommerce: Check if User Has Bought Product in the Last 365 Days

A few snippets ago we introduced the magic WooCommerce inbuilt function “wc_customer_bought_product” – automatically, with a single line of PHP, you can find out if the user has already purchased a product ID.

But what if you need to know if a user has purchased a product ID in the last 365 days?

Well, I wrote a custom function by copying most of the existing code, changing its name and there you go – easy peasy!

The original “wc_customer_bought_product” function in WooCommerce

PHP Snippet: Check if User Has Bought Product ID in the Last 365 Days

/**
 * @snippet       "wc_customer_bought_product" last 365 days
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

function wc_customer_bought_product_last_365( $customer_email, $user_id, $product_id ) {
	global $wpdb;

	$result = apply_filters( 'woocommerce_pre_customer_bought_product_last_365', null, $customer_email, $user_id, $product_id );

	if ( null !== $result ) {
		return $result;
	}

	$transient_name = 'wc_customer_bought_product_last_365_' . md5( $customer_email . $user_id );
	$transient_version = WC_Cache_Helper::get_transient_version( 'woocommerce_reports' );
	$transient_value = get_transient( $transient_name );

	if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) {
		$result = $transient_value['value'];
	} else {
		$customer_data = array( $user_id );

		if ( $user_id ) {
			$user = get_user_by( 'id', $user_id );
			if ( isset( $user->user_email ) ) {
				$customer_data[] = $user->user_email;
			}
		}

		if ( is_email( $customer_email ) ) {
			$customer_data[] = $customer_email;
		}

		$customer_data = array_map( 'esc_sql', array_filter( array_unique( $customer_data ) ) );
		$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );

		if ( count( $customer_data ) === 0 ) {
			return false;
		}

		$date_limit = date( 'Y-m-d H:i:s', strtotime( '-365 days' ) );

		if ( 'yes' === get_option( 'woocommerce_custom_orders_table_enabled' ) ) {
			$statuses = array_map( function ( $status ) { return "wc-$status"; }, $statuses );
			$order_table = $wpdb->prefix . 'wc_orders';
			$user_id_clause = $user_id ? 'OR o.customer_id = ' . absint( $user_id ) : '';
			$sql = "
SELECT DISTINCT product_or_variation_id FROM (
SELECT CASE WHEN product_id != 0 THEN product_id ELSE variation_id END AS product_or_variation_id
FROM {$wpdb->prefix}wc_order_product_lookup lookup
INNER JOIN $order_table AS o ON lookup.order_id = o.ID
WHERE o.status IN ('" . implode( "','", $statuses ) . "')
AND ( o.billing_email IN ('" . implode( "','", $customer_data ) . "') $user_id_clause )
AND o.date_created_gmt >= '$date_limit'
) AS subquery
WHERE product_or_variation_id != 0
";
			$result = $wpdb->get_col( $sql );
		} else {
			$result = $wpdb->get_col(
				"
SELECT DISTINCT product_or_variation_id FROM (
SELECT CASE WHEN lookup.product_id != 0 THEN lookup.product_id ELSE lookup.variation_id END AS product_or_variation_id
FROM {$wpdb->prefix}wc_order_product_lookup AS lookup
INNER JOIN {$wpdb->posts} AS p ON p.ID = lookup.order_id
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND pm.meta_key IN ( '_billing_email', '_customer_user' )
AND pm.meta_value IN ( '" . implode( "','", $customer_data ) . "' )
AND p.post_date >= '$date_limit'
) AS subquery
WHERE product_or_variation_id != 0
"
			);
		}

		$result = array_map( 'absint', $result );

		$transient_value = array(
			'version' => $transient_version,
			'value'   => $result,
		);

		set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
	}

	return in_array( absint( $product_id ), $result, true );
}

Now that the “wc_customer_bought_product_last_365” exists in your functions.php, you can use it in a snippet or shortcode.

if ( wc_customer_bought_product_last_365( 'info@example.com', 1, 246642 ) ) {
   // do something if has purchased in the last 365 days
} else {
   // do something else
}

In regard to the 3 function parameters, you must specify the third (product ID) and at least one of the first two must be not null. These will be valid:

if ( wc_customer_bought_product_last_365( 'info@example.com', '', 246642 ) )
if ( wc_customer_bought_product_last_365( '', 1, 246642 ) )

These won’t be valid:

if ( wc_customer_bought_product_last_365( 'info@example.com', 246642 ) )
if ( wc_customer_bought_product_last_365( 1, 246642 ) )

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

14 thoughts on “WooCommerce: Check if User Has Bought Product in the Last 365 Days

  1. Hi.
    can I check how many times the user bought that product in the last 365 days?

    1. Hi David, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi,
    I don’t need the email address to be checked in this function, user id is enough to be checked.
    what should I change in your code to ignore customer email?

    1. wc_customer_bought_product_last_365( '', 123, 456 );
  3. Hello,

    Thanks for this feature!

    I just have a question because I think I found a bug. A customer connected but without purchase still sees the data. When I do a var_dump it returns true when it should be false since there was no purchase.

    1. Not sure, this would require custom troubleshooting. Function works great. Sorry

  4. Hi Rodolfo,

    How can I use that function? ( wc_customer_bought_product_last_365 )

    Thanks

    1. As a conditional, when you check that against a single product ID

  5. I do not get this to work.
    I got 3 products reflecting 365 days, 30 days and 7 days, 1 customer has bought both 365 days and 7 days, the 7 days are gone (more than 7 days since bought), yet both returns true.
    I added a parameter in the function to pass the days into the function.
    if any of the products are bought within the given days day should “give access” to certain content.
    i check first for 365 days then 30 days and then 7 days.
    shouldnt the 7 days return false?

    1. Hi there, thanks so much for your comment! Yes, this can be fixed, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. You is better!

    1. Thanks!

  7. Thanks a bunch !!!

    1. You’re welcome Nadia!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *