Woocommerce multiple products to cart with one click [duplicate]
Have you ever found yourself wishing you could add multiple products to your WooCommerce cart with just one click? Well, your wish has been granted! In this article, we'll show you a simple script that will enable your customers to do just that.
The Problem: Adding Products One-by-One
Traditionally, WooCommerce users have had to navigate to each product page, select the quantity, and then click "Add to Cart" to add an item to their cart. This can be a time-consuming and frustrating process, especially for customers looking to purchase several products at once.
When a customer wants to buy multiple products, they often have to repeat this process for each item, which can be a significant barrier to conversion. Imagine a shopper who wants to purchase 5 different products – they would have to go through the "Add to Cart" process 5 separate times. This friction in the buyer's journey can lead to abandoned shopping carts and lost sales.
The Solution: Add Multiple Products with One Click
Fortunately, there's a simple solution that can streamline the checkout process for your WooCommerce store. By implementing a small script in your theme's functions.php
file, you can enable your customers to add multiple products to their cart with a single click.
Here's the script you can use:
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart query arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) {
// Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
/*
* Sorry.. if you want non-simple products, you're on your own.
*
* Related: WooCommerce has set the following methods as private:
* WC_Form_Handler::add_to_cart_handler_variable(),
* WC_Form_Handler::add_to_cart_handler_grouped(),
* WC_Form_Handler::add_to_cart_handler_simple()
*
* Why you gotta be like that WooCommerce?
*/
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );
This script works by intercepting the WooCommerce "Add to Cart" action and handling multiple product IDs that are passed in a comma-separated list. It then adds each product to the cart individually, with the final product being handled by the standard WooCommerce "Add to Cart" process.
How to Use the Script
To use this script, simply copy the code above and paste it into your theme's functions.php
file. Once you've done that, you can add multiple products to the cart by using a URL like this:
http://your-site.com/shop/cart/?add-to-cart=3001,3282
Replace 3001
and 3282
with the actual product IDs you want to add to the cart. Separate the IDs with a comma, and the script will handle the rest.
Benefits of the Multiple Products to Cart Feature
Implementing this script in your WooCommerce store can provide several benefits for your customers and your business:
-
Improved Conversion Rates: By reducing the friction in the checkout process, you can expect to see an increase in the number of customers who complete their purchases. This can lead to higher overall revenue for your store.
-
Enhanced User Experience: Customers will appreciate the convenience of being able to add multiple products to their cart with a single click. This can improve their satisfaction with your store and increase the likelihood of repeat business.
-
Increased Average Order Value: When customers can easily add multiple products to their cart, they may be more inclined to purchase additional items, leading to a higher average order value for your store.
-
Reduced Abandoned Carts: By making the checkout process more streamlined, you can expect to see a reduction in the number of abandoned shopping carts, as customers will be less likely to get frustrated and leave your site.
Conclusion
In conclusion, the script we've provided in this article can be a game-changer for your WooCommerce store. By enabling your customers to add multiple products to their cart with a single click, you can improve the overall user experience, boost conversion rates, and drive more revenue for your business.
Ready to get started? Copy the script, add it to your theme's functions.php
file, and start providing your customers with a smoother, more efficient checkout process today. And don't forget, Flowpoint.ai can help you identify and fix any other technical issues that may be impacting your website's conversion rates
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.