Home > Backend Development > PHP Tutorial > How to Determine if a Customer Has Made a Purchase in WooCommerce?

How to Determine if a Customer Has Made a Purchase in WooCommerce?

Linda Hamilton
Release: 2024-11-25 16:54:15
Original
858 people have browsed it

How to Determine if a Customer Has Made a Purchase in WooCommerce?

How to Verify if a Customer Has Made a Purchase in WooCommerce

In WooCommerce, providing incentives to loyal customers who have previously purchased is a common practice. To effectively implement such strategies, it becomes necessary to verify if a customer has made any purchases before.

Checking Purchase History

To ascertain a customer's purchase history, WooCommerce provides a light and efficient conditional function called has_bought(). This function analyzes customer data to determine whether they have previously made a purchase.

The function handles both registered users and guests:

  • Registered users: If no user ID is specified, the function uses the ID of the currently logged-in user.
  • Guests: For guests, the function requires their billing email as an argument.

Implementing the Function

function has_bought( $value = 0 ) {
    if ( ! is_user_logged_in() && $value === 0 ) {
        return false;
    }

    global $wpdb;

    // Based on user ID (registered users)
    if ( is_numeric( $value) ) { 
        $meta_key   = '_customer_user';
        $meta_value = $value == 0 ? (int) get_current_user_id() : (int) $value;
    } 
    // Based on billing email (Guest users)
    else { 
        $meta_key   = '_billing_email';
        $meta_value = sanitize_email( $value );
    }
    
    $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 a boolean value based on orders count
    return $count > 0;
}
Copy after login

Usage Examples

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>';
Copy after login

Guest with Billing Email:

$email = '[email&#160;protected]';
if( has_bought( $email ) )
        echo '<p>customer have already made a purchase</p>';
    else
        echo '<p>Customer with 0 purchases</p>'
Copy after login

The above is the detailed content of How to Determine if a Customer Has Made a Purchase in WooCommerce?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template