I am using the following code to allow adding multiple items via url (...com/checkout/?add-to-cart=632:11,422:7
) to the woocommerce cart. Somehow it does not empty the logged in OR logged out users cart before adding the new items using WC()->cart->empty_cart();
. Any idea why?
// Function to allow adding mulitple items via url in this format to checkout or cart ...com/checkout/?add-to-cart=632:11,422:7
function add_multiple_products_to_cart_9R_M2asdf() {
// Ensure WooCommerce is installed and the 'add-to-cart' query parameter is present and formatted correctly.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Empty the cart before adding new items otherwise it just adds to the ones added before...
WC()->cart->empty_cart();
// Remove WooCommerce's default add_to_cart_action hook.
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
// Split the product IDs and quantities.
$product_entries = explode( ',', sanitize_text_field( $_REQUEST['add-to-cart'] ) );
foreach ( $product_entries as $entry ) {
// Split each entry into product ID and quantity.
list($product_id, $quantity) = array_map('absint', explode( ':', $entry ) + [null, 1]);
if ( ! $product_id ) {
continue;
}
// Set the quantity in the request.
$_REQUEST['quantity'] = $quantity;
// Handle the final item using WooCommerce's default method.
if ( end($product_entries) === $entry ) {
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
// Process the product.
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', $product_id );
$product = wc_get_product( $product_id );
if ( ! $product ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $product->get_type(), $product );
// Handle different product types.
if ( 'variable' === $add_to_cart_handler ) { // Variabl Products
woo_invoke_private_method_93gK_M3q( 'WC_Form_Handler', 'add_to_cart_handler_variable', $product_id );
} elseif ( 'grouped' === $add_to_cart_handler ) { // Grouped Products
woo_invoke_private_method_93gK_M3q( 'WC_Form_Handler', 'add_to_cart_handler_grouped', $product_id );
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) { // Custom Handler
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler );
} else { // simple product
woo_invoke_private_method_93gK_M3q( 'WC_Form_Handler', 'add_to_cart_handler_simple', $product_id );
}
}
}
add_action( 'wp_loaded', 'add_multiple_products_to_cart_9R_M2asdf', 15 );
/**
* Invoke private method of a class.
*
* @param string $class_name
* @param string $method_name
* @param mixed ...$args
* @return mixed
*/
function woo_invoke_private_method_93gK_M3q( $class_name, $method_name, ...$args ) {
if ( version_compare( phpversion(), '5.3', '<' ) ) {
throw new Exception( 'PHP version does not support ReflectionClass::setAccessible()', __LINE__ );
}
$reflection = new ReflectionClass( $class_name );
$method = $reflection->getMethod( $method_name );
$method->setAccessible( true );
return $method->invoke( null, ...$args );
}