Pawan Kumawat

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.

Categories

Related Blogs

Upload Any File in My Account Registration Form

To allow users to upload a file in the My Account registration form in WooCommerce, you’ll need to customize the registration form and add a file upload field. We’ll use a custom function and a hook to achieve this. Here’s a step-by-step guide:

WooCommerce: Check if Product ID is in the Cart

To check if a specific product ID is in the cart in WooCommerce, you can use the `woocommerce_before_cart` action hook, which is fired before the cart page is displayed. Here’s how you can achieve this:

Disable Repeat Purchase Of Product

To disable repeat purchase of products in WooCommerce, you can implement a custom solution using a combination of code snippets and WooCommerce hooks. The idea is to prevent customers from adding the same product to the cart if it already exists in the cart. Below are the steps to achieve this: