Home  >  Article  >  Backend Development  >  How to Check if a WooCommerce User Has Made Past Purchases?

How to Check if a WooCommerce User Has Made Past Purchases?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-14 14:32:02947browse

How to Check if a WooCommerce User Has Made Past Purchases?

Checking Purchase History in WooCommerce

Problem:

You wish to create a WooCommerce plugin that offers exclusive deals to customers with a purchase history. The challenge is determining whether a user has made a purchase in the past.

Solution:

Checking Past Purchases for Customers

To check if a customer has made any past purchases, you can leverage a custom function that seamlessly handles both registered users and guest users:

function has_bought( $value = 0 ) {
    // Handling for registered users
    if ( is_numeric( $value) ) { 
        // Use current user ID if not provided
        $meta_key   = '_customer_user';
        $meta_value = $value == 0 ? (int) get_current_user_id() : (int) $value;
    } 
    // Handling for guest users
    else { 
        $meta_key   = '_billing_email';
        $meta_value = sanitize_email( $value );
    }
    
    global $wpdb;
    
    // Determine if any paid orders exist
    $paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    $count = $wpdb->get_var( $wpdb->prepare("
        SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        WHERE p.post_status IN ( 'wc-" . implode( ""','wc-", $paid_order_statuses ) . ""' )
        AND p.post_type LIKE 'shop_order'
        AND pm.meta_key = '%s'
        AND pm.meta_value = %s
        LIMIT 1
    ", $meta_key, $meta_value ) );

    // Return true if at least one paid order exists
    return $count > 0;
}

Usage Examples:

Example 1: Check for purchases by the currently logged-in customer

if( has_bought() )
    echo '<p>You have already made a purchase</p>';
else
    echo '<p>Welcome, for your first purchase you will get a discount of 10%</p>';

Example 2: Check for purchases by a specific user ID

// Define the user ID
$user_id = 85;

if( has_bought( $user_id ) )
        echo '<p>customer have already made a purchase</p>';
    else
        echo '<p>Customer with 0 purchases</p>';

Example 3: Check for purchases by a guest user's billing email

// Define the billing email (string)
$email = '[email protected]';

if( has_bought( $email ) )
        echo '<p>customer have already made a purchase</p>';
    else
        echo '<p>Customer with 0 purchases</p>'

The above is the detailed content of How to Check if a WooCommerce User Has Made Past Purchases?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn