Adding multiple items to WooCommerce cart at once
I found the answer!
Simply add the following script to your theme's functions.php:
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery 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 );
And then you can simply use http://shop.com/shop/cart/?add-to-cart=3001,3282
to add multiple items at once. Put a comma between different IDs.
Thanks to dsgnwrks for the solution.
Automatically add multiple products to cart on visit WooCommerce
There are actually two ways you can do this, at least. You can either call the add_to_cart
function more than once, or create a loop. Both ways are down here:
Method 1
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
WC()->cart->add_to_cart( $product_id2 );
WC()->cart->add_to_cart( $product_id3 );
WC()->cart->add_to_cart( $product_id4 );
}
}
}
Method 2
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
Do note that you should create a new array called articles
holding all the IDs from the desired products. Another thing that is going to bother you then is checking whether the cart holds more than 0 items, and checking if all of them are in there.
Method 2 would look something like this:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$articles = array(64);
$found = false;
// check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if (($key = array_search($_product->id, $articles)) !== false)
unset($articles[$key]);
}
// if product not found, add it
if ( count($articles) > 0 ) {
foreach ($articles as $article) {
WC()->cart->add_to_cart($article);
}
}
} else {
// if no products in cart, add it
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
}
}
}
Add multiple products to cart if they are not already in cart
As in the code you are using you have woocommerce_add_to_cart_validation
filter hook inside it, you can use it in a custom hooked function to check if products rare already in cart with something like:
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
function check_product_added_to_cart( $passed, $product_id, $quantity) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['data']->get_id() == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
return $passed;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and this normally should works with your customization…
For product quantities, you can use the
$quantity
argument with$cart_item['quantity']
in some conditions…
Related Topics
Difference Between Eloquent\Model::Get() and All()
Changing a Global Variable from Inside a Function PHP
How to Employ "If Exists" for Creating or Dropping an Index in MySQL
PHP Convert Decimal into Fraction and Back
Woocommerce Add to Cart Button Redirect to Checkout
Warning: MySQLi_Real_Escape_String() Expects Exactly 2 Parameters, 1 Given... What I Do Wrong
I'm Getting This Error Code: Invalid Argument Supplied for Foreach()
Using SQLite3 in PHP How to Count the Number of Rows in a Result Set
How to Get Info on Sent PHP Curl Request
How Many Days Until X-Y-Z Date
MySQL Query In() Clause Slow on Indexed Column
How to Set Session Timeout in Laravel
Windows Cmd.Exe "The System Cannot Find the Path Specified."