Disable Repeat Purchase of Product in WooCommerce

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.

Scroll to Top

Request A Quote