Check if a User Has Already Paid for a Product in Woocommerce

In this post we will show you how you can check if a user has already paid for a Woocommerce product and how to test the code. You can use the code for a plugin for example and use the result as you like. The example below is just to test the code.

 

This is the code to see if a user already paid for specific products:

function arrow_design_CheckWhetherUserPaid() {
$bought = false;
// Set HERE ine the array your specific target product IDs
$prod_arr = array( '21', '67' );
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status “completed”
));
foreach ( $customer_orders as $customer_order ) {
// Updated compatibility with WooCommerce 3+
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$order = wc_get_order( $customer_order );
// Iterating through each current customer products bought in the order
foreach ($order->get_items() as $item) {
// WC 3+ compatibility
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$product_id = $item['product_id'];
} else { $product_id = $item->get_product_id();
}
// Your condition related to your 2 specific products Ids
if ( in_array( $product_id, $prod_arr ) ) {
$bought = true;
}
}
}
// return “true” if one the specifics products have been bought before by customer
return $bought;
}

Example / Code Testing

We will test the code by changing the product IDs to the current product being displayed and showing some text on the Woocommerce Single Product Pages.

 

You can test it and show the text by adding the following code to your functions.php file:

function arrow_design_CheckWhetherUserPaid() {
$bought = false; 
// set product ID to the current product being displayed
$prod_arr = array( get_the_ID());

// build array of customer orders based on the current user
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // only orders with status “completed”
));

foreach ( $customer_orders as $customer_order ) {
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

$order = wc_get_order( $customer_order ); // iterating through each current customer products bought in the order
foreach ($order->get_items() as $item) {
if ( version_compare( WC_VERSION, '3.0', '<' ) ) 
{
$product_id = $item['product_id']; 
}// end if
else
{
$product_id = $item->get_product_id();
}// end else

if ( in_array( $product_id, $prod_arr ) )
{
$bought = true;
}// end if 
// mark variable $bought as true, if the order product id, is in the array of all products, purchased by current user
}// end for each order get items
}// end for each customer order

if($bought==true){
echo '<p>You Have Previously Purchased This Product</p>';
}// end if
else
{
echo '<p>You Have NOT Previously Purchased This Product</p>';
}// end else

}// end function

add_action( 'woocommerce_before_single_product', 'arrow_design_CheckWhetherUserPaid', 10 );

This is how the Single Product Page looks like after changing the code:

Check if a User Has Already Paid for a Product in Woocommerce

 

It says “You Have NOT Previously Purchased This Product”. This is shown because the order is still “Processing” and not set to “Completed”. Set the order to “Completed” by going to WooCommerce –> Orders on the left menu and click into the order. Then change the order status like this:

Check if a User Has Already Paid for a Product in Woocommerce

 

After saving the order and refreshing the page, this is shown on the Single Product Page:

Check if a User Has Already Paid for a Product in Woocommerce

 

Not is says “You Have Previously Purchased This Product”.

Conclusion

We hope this Check if a User Has Already Paid for a Product in Woocommerce post was helpful and you are now able to integrate this also on your website! You can find more articles here:

 

Read another Woocommerce post : ‘Change “From” Email Address

 

Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.

We do it all, so you don’t have to!

Related Posts

Woocommerce Account Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce Single Product Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce PHP – Product Categories by Product ID

In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is is on the shop page, in the cart, in an order & more.

…We do more, so you can do less 🙂

 

Leave a message and we will be straight back to you!

Pin It on Pinterest

Share This