One of the common challenges in managing a WooCommerce store is avoiding duplicated product orders—either by mistake or through intentional abuse. Users may click multiple times, refresh the page with an ?add-to-cart URL, or try to reorder free products they’ve already claimed.
In this guide, we’ll explore three custom WooCommerce scripts that help prevent duplicate orders and provide better control over how products are added to the cart. Each solution is tailored to a different scenario, and together they form a robust defense mechanism for your online store.
Why Do Duplicate Orders Happen?
There are several scenarios that can lead to duplicated orders:
- A product is added multiple times via URL parameters.
- A free product (like a digital download) is re-purchased without restriction.
- A user adds more than one unit of a product that should only be purchased once (e.g., a license or limited offer).
Luckily, WooCommerce is highly extensible, and with the right hooks, we can add server-side logic to prevent all of these issues.
Let’s break it down.
Script 1: Mark Products as “Sold Individually” via Meta Fields
This script enforces a rule where specific products (or variations) can only be purchased one at a time—no duplicates or multiple quantities in the cart. It’s particularly useful for:
- License keys
- Event tickets
- Samples or free items
- Exclusive digital products
function ql_woocommerce_is_sold_individually( $return, $product ) {
switch ( $product->get_type() ) {
case 'variation':
if ( $product->get_meta( 'variation_maximum_allowed_quantity' ) == 1 ) {
return true;
}
break;
case 'simple':
if ( $product->get_meta( 'maximum_allowed_quantity' ) == 1 ) {
return true;
}
break;
}
return $return;
}
add_filter( 'woocommerce_is_sold_individually', 'ql_woocommerce_is_sold_individually', 10, 2 );
This hook checks for a custom meta field (maximum_allowed_quantity
) set to 1
. If found, WooCommerce automatically prevents users from selecting more than one unit or adding the product to the cart multiple times.
How to Use:
- Edit any product or variation in WooCommerce.
- Add the custom field
maximum_allowed_quantity
(orvariation_maximum_allowed_quantity
) and set its value to1
. - Save the product.
That’s it—WooCommerce will now treat that product as “Sold Individually.”
Script 2: Block Repeat Purchase of Free Variations
This script prevents a user from buying a free variation more than once. It’s ideal for:
- Free eBooks or downloads
- One-time promo items
- Registration tickets with a price of $0
function ql_disable_repeat_purchase_for_free_products( $passed, $product_id, $quantity, $variation_id = 0 ) {
$current_user = wp_get_current_user();
// Use the variation if it exists, otherwise use the simple product
$product = $variation_id > 0 ? wc_get_product( $variation_id ) : wc_get_product( $product_id );
$target_id = $variation_id > 0 ? $variation_id : $product_id;
// Check if product is free
if ( floatval( $product->get_price() ) == 0 ) {
// Block if already purchased
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $target_id ) ) {
wc_add_notice( sprintf( __( 'You have already purchased "%s" and cannot purchase it again.', 'ql' ), $product->get_name() ), 'error' );
return false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'ql_disable_repeat_purchase_for_free_products', 10, 4 );
This function uses WooCommerce’s native wc_customer_bought_product()
to check if the current logged-in user already purchased the free variation. If so, it blocks the add-to-cart action and shows an error notice.
Important Notes:
- The user must be logged in for this validation to work.
- The price of the variation must be exactly
0
.
Script 3: Prevent Duplicate Add-to-Cart from URL Parameters
WooCommerce supports adding products to the cart via URL like this:
/cart/?add-to-cart=123
The problem? If a user refreshes the page or shares the link, the product gets added again. This can lead to duplicates in the cart unintentionally.
This script intercepts that behavior on the cart or checkout page and removes the query parameters if the product is already in the cart.
function ql_prevent_duplicate_add_to_cart_redirect() {
if ( ( is_cart() || is_checkout() ) && isset( $_GET['add-to-cart'] ) ) {
$current_url = home_url( add_query_arg( NULL, NULL ) );
$product_id = intval( $_GET['add-to-cart'] );
$variation_id = isset( $_GET['variation_id'] ) ? intval( $_GET['variation_id'] ) : 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $product_id && ( $variation_id == 0 || $cart_item['variation_id'] == $variation_id ) ) {
wp_redirect( remove_query_arg( array( 'add-to-cart', 'variation_id' ), $current_url ) );
exit;
}
}
}
}
add_action( 'template_redirect', 'ql_prevent_duplicate_add_to_cart_redirect' );
This code checks if a product or variation is already in the cart and silently redirects the user to the same page without the add-to-cart
parameter, preventing duplicate additions.
When to Use:
- If you run marketing campaigns using URLs with
add-to-cart
- If you noticed users refreshing their cart and accidentally duplicating items
- On single-product landing pages with auto-add behavior
Putting It All Together
These three scripts can be used together for maximum control over WooCommerce cart behavior:
- Limit product quantity to 1 with meta fields
- Prevent repeat orders of free variations
- Remove duplicate
add-to-cart
actions from URLs
You can add all three to your functions.php
file or include them in a custom plugin.
Bonus Tip: Make It Admin-Friendly
You can enhance usability by:
- Exposing the
maximum_allowed_quantity
field in the WooCommerce admin. - Using a plugin like ACF or writing your own
meta_box
to toggle it via checkbox. - Extending the variation editor interface to manage these meta fields more easily.
Final Thoughts
WooCommerce gives you full control over your eCommerce workflow, but it’s up to developers to fine-tune the behavior to fit real-world business needs.
By implementing these scripts, you’ll:
- Prevent order abuse on free items
- Ensure limited products are only bought once
- Avoid unwanted cart duplication
A better user experience means fewer headaches for you and more trust from your customers.