Disable Repeat Purchase of Product in WooCommerce

To prevent customers from purchasing the same product multiple times in WooCommerce, you can implement a custom solution that uses WooCommerce hooks and code snippets. The idea is to check whether the product already exists in the cart before allowing it to be added again. Below are the steps to achieve this functionality.

 

Step 1: Disable Repeat Purchase

Add the following code to your theme’s functions.php file or a custom plugin. This code checks if the product being added to the cart already exists in the cart and prevents the product from being added again. If it already exists, an error message will be shown:

function disable_repeat_purchase( $valid, $product_id, $quantity ) {
    if ( ! WC()->cart->is_empty() ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item['product_id'] === $product_id ) {
                wc_add_notice( __( 'This product is already in the cart. You cannot add it again.', 'your-text-domain' ), 'error' );
                return false;
            }
        }
    }
    return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'disable_repeat_purchase', 10, 3 );

Step 2: Display Error Message

The code uses wc_add_notice() to display an error message if the customer tries to add a product that is already in the cart. The error message can be customized by changing the message inside the wc_add_notice() function. Ensure to replace 'your-text-domain' with the appropriate text domain for your theme or plugin.

With this code in place, customers will not be able to add the same product to the cart more than once. If they attempt to do so, an error message will be displayed to inform them that the product is already in the cart.

Important Considerations

Note that this functionality only works for the current session. If the customer empties their cart or returns to the store later, they can add the product to the cart again.

It’s essential to thoroughly test this feature to ensure it works as expected and does not interfere with other parts of your WooCommerce store. Make sure that it integrates well with your theme and other customizations.

Related Blogs

How to hide particular category product on shop

To hide a particular category on the shop page using the `woocommerce_product_query` hook, you can modify the query parameters to exclude the category you want to hide. This approach allows you to customize the product query directly without modifying the main query.

Disable plugin update in WordPress

While it’s generally not recommended to disable plugin updates in WordPress due to security and functionality improvements, there might be specific cases where you need to prevent certain plugins from updating. Keep in mind that doing so may leave your site vulnerable to security issues and may cause compatibility problems with future WordPress versions.

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.