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:

Step 1: Disable Repeat Purchase
Add the following code to your theme’s `functions.php` file or a custom plugin. This code will check if the product being added to the cart already exists in the cart. If it does, it will display an error message and prevent the product from being added again.

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 above code uses the `wc_add_notice()` function to display an error message when the customer attempts to add a product that already exists in the cart. You can customize the error message to your liking by modifying the text inside the `wc_add_notice()` function.

Replace `’your-text-domain’` with the appropriate text domain used for translation in your theme or plugin.

With the above code in place, customers won’t be able to add the same product to the cart more than once. If they try to do so, an error message will be displayed, informing them that the product is already in the cart.

Please note that this code only prevents adding the same product to the cart in the same session. If the customer empties the cart and tries to add the product again later or in a different session, they will be able to do so.

Remember to thoroughly test the functionality to ensure it works as expected and does not interfere with other aspects of your WooCommerce store.

Related Blogs

How To Limit Search To Post Titles?

I added a text field to the posts filter form and I use s= parameter to make the search work. But how to search only in the title of the post (not in the content)? how to limit search to post titles? How to search only by Post Title?

Shortcodes included with Dokan

Dokan is a popular multi-vendor marketplace plugin for WooCommerce that allows you to create and manage a marketplace where multiple vendors can sell their products. Dokan provides several shortcodes that you can use to display various elements and functionalities on your marketplace pages. Here are some of the essential shortcodes provided by Dokan:

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.