WooCommerce: add multiple products to cart via url

webroomtech.com
4 min readNov 12, 2019

WooCommerce gives you the flexibility to do almost anything. This includes creating a custom link to add several products to the cart.

Use cases

As a business owner you may want to give the customer the opportunity to add several products to the cart with just one link. Lets say your support team arranges a sale and instead navigating the customer to a product pages. Another example to use adding products to cart directly via URL is when you create a landing page for product or service and add the link to the CTA buttons, navigating the customers directly to cart/checkout.

PHP snippet: add multiple products to cart via link

For more comprehensive look at the PHP snippet go to the original publication here.

function webroom_add_multiple_products_to_cart( $url = false ) {
// 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 $id_and_quantity ) {
// Check for quantities defined in curie notation (<product_id>:<product_quantity>)

$id_and_quantity = explode( ‘:’, $id_and_quantity );
$product_id = $id_and_quantity[0];

$_REQUEST[‘quantity’] = ! empty( $id_and_quantity[1] ) ? absint( $id_and_quantity[1] ) : 1;

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( $url );
}

$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->get_type(), $adding_to_cart );

// Variable product handling
if ( ‘variable’ === $add_to_cart_handler ) {
woo_hack_invoke_private_method( ‘WC_Form_Handler’, ‘add_to_cart_handler_variable’, $product_id );

// Grouped Products
} elseif ( ‘grouped’ === $add_to_cart_handler ) {
woo_hack_invoke_private_method( ‘WC_Form_Handler’, ‘add_to_cart_handler_grouped’, $product_id );

// Custom Handler
} elseif ( has_action( ‘woocommerce_add_to_cart_handler_’ . $add_to_cart_handler ) ){
do_action( ‘woocommerce_add_to_cart_handler_’ . $add_to_cart_handler, $url );

// Simple Products
} else {
woo_hack_invoke_private_method( ‘WC_Form_Handler’, ‘add_to_cart_handler_simple’, $product_id );
}
}
}

// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( ‘wp_loaded’, ‘webroom_add_multiple_products_to_cart’, 15 );

/**
* Invoke class private method
*
* @since 0.1.0
*
* @param string $class_name
* @param string $methodName
*
* @return mixed
*/
function woo_hack_invoke_private_method( $class_name, $methodName ) {
if ( version_compare( phpversion(), ‘5.3’, ‘<’ ) ) {
throw new Exception( ‘PHP version does not support ReflectionClass::setAccessible()’, __LINE__ );
}

$args = func_get_args();
unset( $args[0], $args[1] );
$reflection = new ReflectionClass( $class_name );
$method = $reflection->getMethod( $methodName );
$method->setAccessible( true );

//$args = array_merge( array( $class_name ), $args );
$args = array_merge( array( $reflection ), $args );
return call_user_func_array( array( $method, ‘invoke’ ), $args );
}

How to use

Copy and paste the above snippet directly to your child theme’s functions.php. That’s all. Now lets see the front end part. Your single URL for adding multiple products to cart will look like:

https://www.example.com/cart/?add-to-cart=12345,43453

Take a look at the ?add-to-cart=12345,43453 part. Here is where you add your product ids. If you want to add twice the same product, just type twice it’s product id:

https://www.example.com/cart/?add-to-cart=12345,12345,12345

or add the following string: &quantity=3, so the URL will become

https://www.example.com/cart/?add-to-cart=12345&quantity=3

How to add product to cart via url and redirect to checkout

https://www.example.com/checkout/?add-to-cart=12345

Note: for this to work you must tick the “Enable AJAX add to cart buttons on archives” option under WooCommerce –> Settings –> Products -> General and also disable “Redirect to the cart page after successful addition”.

How to add Variable Products to cart via URL

All you need to do is get the variation ID and place it in the ?add-to-cart= url. Remember you can also add &quantity=3 so you add the product several times. Redirecting to Cart or Checkout is also valid here.

How to get Product id in WooCommerce

Go ahead and edit a product. Now take a look at the url, it will look like: 1

https://www.example.com/wp-admin/post.php?post=1493&action=edit

You will find the product id in ?post=1493.

If you want to further customize your WooCommerce store check out our other articles:

The article was originally published at webroom.tech

--

--

webroomtech.com
0 Followers

webroom.tech is created and written with one purpose: to help anyone developing their awesome WordPress and WooCommerce website.