WooCommerce allows products to be added to the cart through URLs, but what if you need to add multiple products with specific quantities in one link? This guide walks you through how to create a custom add-to-cart URL that supports multiple products, quantities, and even variations or grouped products.
We’ll also show how to redirect customers directly to the cart page after adding products using the same URL.
Why Use a Multiple Product Add-to-Cart URL?
This feature is particularly useful for:
- Pre-Configured Bundles: Let customers quickly add several items to their cart in one click.
- Order Forms: Streamline bulk purchases by generating pre-filled order links.
- Marketing Campaigns: Share custom URLs in emails or on social media to promote specific product combinations.
Step 1: Create the Custom Add-to-Cart URL
Below is the general format for adding multiple products and quantities via URL:
https://yourwebsite.com/?add-to-cart=123&quantity=3&add-to-cart=5487&quantity=2
Explanation
add-to-cart=123: Adds the product with ID 123.quantity=3: Adds 3 units of the product.add-to-cart=5487: Adds another product with ID 5487.quantity=2: Adds 2 units of the second product.
However, WooCommerce only processes the first product in such a URL. To support multiple products and quantities, we’ll modify this behaviour with custom code.
Step 2: Add Code to Handle Multiple Products in the URL
Add this code to your theme’s functions.php file or in your child theme.
/*
* Snippet: How to Add Multiple Products with Quantities to the Cart Using a URL in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1622
* Tested with WooCommerce 10.7.0
* "This function adds multiple products and quantities to the WooCommerce cart via URL"
*/
function wcsuccess_add_multiple_products_to_cart_via_url() {
if ( ! isset( $_GET['add-to-cart'] ) ) {
return;
}
$product_ids = $_GET['add-to-cart']; // WooCommerce processes this as a single or repeated key
if ( ! is_array( $product_ids ) ) {
$product_ids = array( $product_ids ); // Ensure it's an array if only one product is passed
}
foreach ( $product_ids as $index => $product_id ) {
$quantity = isset( $_GET['quantity'][$index] ) ? intval( $_GET['quantity'][$index] ) : 1;
if ( isset( $_GET['variation_id'][$index] ) ) {
$variation_id = intval( $_GET['variation_id'][$index] );
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
} else {
WC()->cart->add_to_cart( $product_id, $quantity );
}
}
if ( isset( $_GET['redirect_to_cart'] ) && $_GET['redirect_to_cart'] == 'yes' ) {
wp_redirect( wc_get_cart_url() );
exit;
}
}
add_action( 'template_redirect', 'wcsuccess_add_multiple_products_to_cart_via_url' );
How the Code Works
- Process Multiple Products: The code loops through multiple product IDs and quantities from the URL.
- Variation Support: If a
variation_idis provided, the code ensures that the correct variation is added to the cart. - Redirect to Cart: If
?redirect_to_cart=yesis added to the URL, the customer is redirected to the cart page after products are added.
Step 3: Create Example URLs
Add Simple Products with Quantities
https://yourwebsite.com/cart/?add-to-cart=123&quantity=2&add-to-cart=456&quantity=1
Add Variable Products
https://yourwebsite.com/cart/?add-to-cart=123&quantity=2&variation_id=456&add-to-cart=789&quantity=3&variation_id=1011
Redirect to Cart After Adding Products
https://yourwebsite.com/cart/?add-to-cart=123&quantity=2&variation_id=456&add-to-cart=789&quantity=3&variation_id=1011
&redirect_to_cart=yes
Step 4: Use with Grouped Products
To add grouped products, include the group’s product ID with individual quantities for each grouped item.
https://yourwebsite.com/?add-to-cart[]=555&quantity[]=2&add-to-cart[]=556&quantity[]=3
This method works for any product type WooCommerce supports, including simple, variable, and grouped products.
Step 5: Handle Multiple Products and Variations
/*
* Snippet: How to Add Multiple Products with Quantities to the Cart Using a URL in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1622
* Tested with WooCommerce 10.7.0
* "This function adds multiple products with variations and quantities to the cart via URL"
*/
function wcsuccess_add_multiple_products_with_variations_to_cart_via_url() {
if ( ! isset( $_GET['add-to-cart'] ) ) {
return;
}
$product_ids = $_GET['add-to-cart']; // Get the product IDs
if ( ! is_array( $product_ids ) ) {
$product_ids = array( $product_ids ); // Ensure the product IDs are an array
}
$quantities = isset( $_GET['quantity'] ) ? (array) $_GET['quantity'] : [];
$variation_ids = isset( $_GET['variation_id'] ) ? (array) $_GET['variation_id'] : [];
foreach ( $product_ids as $index => $product_id ) {
$quantity = isset( $quantities[$index] ) ? intval( $quantities[$index] ) : 1;
// Check if a variation ID is provided for the product
if ( isset( $variation_ids[$index] ) && ! empty( $variation_ids[$index] ) ) {
$variation_id = intval( $variation_ids[$index] );
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
} else {
// Add as a simple product if no variation ID is specified
WC()->cart->add_to_cart( $product_id, $quantity );
}
}
// Optional redirect to cart
if ( isset( $_GET['redirect_to_cart'] ) && $_GET['redirect_to_cart'] == 'yes' ) {
wp_redirect( wc_get_cart_url() );
exit;
}
}
add_action( 'template_redirect', 'wcsuccess_add_multiple_products_with_variations_to_cart_via_url' );
https://yourwebsite.com/cart/?add-to-cart=123&quantity=2&variation_id=456&add-to-cart=789&quantity=3&variation_id=1011&redirect_to_cart=yes
Best Use Cases for Multiple Product URLs
- Order Forms: Create a pre-filled order form with multiple products and quantities.
- Bundles and Promotions: Promote special bundles via links shared in emails or on social media.
- Fast Checkout Experience: Save customers time by offering a one-click cart option.
Conclusion
Adding multiple products and quantities to the cart using a single URL provides a smooth and efficient user experience. With the custom code provided, you can extend WooCommerce’s default behaviour to handle multiple products, variations, and even redirect customers directly to the cart page.
Test these changes on a staging site before applying them to your live store. Use a child theme to safeguard your customisations from theme updates. For further WordPress customisations, explore our wp-config generator for easier configurations.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

