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.

Leave a Comment

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