Only Allow to Buy a Product Once in WooCommerce

To restrict customers from buying a WooCommerce product more than once, you can implement a custom solution that prevents multiple purchases of the same product for a single user.

To only allow customers to buy a product once using the wc_customer_bought_product, is_user_logged_in, and woocommerce_is_purchasable functions in WooCommerce, follow these steps:

 

Step 1: Create a Custom Function Add the following code to your theme’s functions.php file or a custom plugin:

function custom_restrict_duplicate_product_purchase($purchasable, $product_id, $user_id)
{
    // Check if the user is logged in
    if (is_user_logged_in()) {
        // Check if the product is already purchased by the user
        if (wc_customer_bought_product('', $user_id, $product_id)) {
            $purchasable = false;
        }
    }

    return $purchasable;
}
add_filter('woocommerce_is_purchasable', 'custom_restrict_duplicate_product_purchase', 10, 3);

Step 2: Save Changes and Test Save the changes to your functions.php file or custom plugin. Now, when a logged-in customer tries to purchase a product they’ve already bought, the purchase will be restricted, and they won’t be able to add the product to their cart again.

The wc_customer_bought_product function checks if a specific user has already purchased a particular product. We use this function in conjunction with the is_user_logged_in function to check if the user is logged in before restricting the purchase.

Please note that this solution only prevents customers from purchasing the same product again if they are logged in. If you want to restrict purchases based on other criteria or for guest users, additional custom logic may be required.

Also, bear in mind that the wc_customer_bought_product function checks the user’s orders’ purchase history. It does not prevent customers from buying the same product again using different accounts or as guest users. If you need more advanced functionality or stricter restrictions, you might need to consider using plugins or custom development tailored to your specific needs.

Share This On :

Leave a Comment

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