Batching Cart Items in WooCommerce via URL Parameters
By default, WooCommerce expects a single add-to-cart parameter when constructing a direct link to add products. Attempting to stack multiple items using an ampersand, such as ?add-to-cart=3001&add-to-cart=2002, results in the browser overwriting the previous value rather than aggregating them. The final URL sent to the server will only contain the last ID in the query string. This limitation exists because standard GET requests treat repeated parameter names as a list of values, but WooCommerce's cart logic specifically isolates the single add-to-cart key.
To bypass this restriction and add multiple items programmatically or via a direct URL without writing custom plugin code, you must leverage the qty (quantity) parameter combined with a specific request structure that respects how the server parses repeated keys. However, relying solely on standard URL parameters often fails because WooCommerce's AJAX handler expects a single integer for the cart action.
The robust solution involves utilizing the cart/add endpoint directly or ensuring your frontend logic sends the data as an array within a JSON payload if using AJAX. If you are constrained to a pure URL redirect without backend hooks, there is a workaround involving the add-to-cart parameter repeated, but it requires a specific server-side configuration or a plugin that intercepts the request and iterates through the array of IDs provided in the query string.
Without custom development, the most reliable method to add multiple items at once from a URL is to construct a link that points to the cart page while using a data attribute approach on client-side JavaScript, or to accept that standard GET requests are limited to one item per add-to-cart key unless modified. If you control the server environment, you can implement a filter hook in your functions.php file to detect multiple add-to-cart parameters and process them sequentially.
Here is how you can handle this logic in a custom plugin or theme snippet:
add_action( 'wp_footer', function() {
// This approach requires client-side JS, but let's look at server-side handling first.
}, 99 );
A more direct implementation for developers who want to modify the core behavior involves hooking into the woocommerce_add_to_cart action or overriding the cart URL generation logic. However, if you strictly need a URL-based solution that works out of the box with minimal code changes, you might need to enable the "Add multiple items" feature via a filter.
One effective way to achieve this without writing a full plugin is to use a temporary cookie or session variable approach before redirecting to the cart. However, the cleanest technical implementation for adding multiple IDs directly via URL involves appending them as distinct parameters and ensuring the server treats them as an array rather than overwriting the first value.
If you are using a headless setup or an API-driven frontend, you would typically send a POST request to wc_add_to_cart with an array of items:
POST /wp-json/wc/v3/cart/add
{
"items": [
{ "product_id": 3001, "qty": 1 },
{ "product_id": 2002, "qty": 1 }
]
}
For traditional PHP environments where you must stick to GET requests, the standard behavior cannot be overridden without modifying the core logic or using a third-party solution. Some advanced setups allow for passing an array in the add-to-cart parameter by URL-encoding it, but this is not universally supported by all WooCommerce versions and themes.
If your goal is to build a bulk add feature for users who prefer direct links, consider implementing a custom shortcode that generates a composite URL string using JavaScript to push multiple items into a queue before redirecting. This ensures that the final request contains the necessary data structure to handle multiple additions in a single transaction.
For developers looking to integrate this functionality into an existing site without deep customization, checking if your WooCommerce version supports the woocommerce_add_to_cart_redirect hook might provide insights into how redirects are handled after batch additions. If you require enterprise-grade cart management or need to customize core behavior extensively, exploring specialized extensions or consulting with a development agency can streamline the implementation.
Ultimately, the default behavior of overwriting values in GET requests is a safeguard against malformed data, but understanding the underlying mechanism allows you to architect workarounds that fit your specific workflow. Whether through custom filters, API calls, or client-side aggregation, adding multiple items at once requires moving beyond the single-parameter limitation inherent in standard URL structures.