stackoverflow<\/a>.<\/p>\n\n\n\nfunction woocommerce_maybe_add_multiple_products_to_cart() {\n\/\/ Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.\nif ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {\n return;\n}\n\n\/\/ Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).\nremove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );\n\n$product_ids = explode( ',', $_REQUEST['add-to-cart'] );\n$count = count( $product_ids );\n$number = 0;\n\nforeach ( $product_ids as $product_id ) {\n if ( ++$number === $count ) {\n \/\/ Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.\n $_REQUEST['add-to-cart'] = $product_id;\n\n return WC_Form_Handler::add_to_cart_action();\n }\n\n $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );\n $was_added_to_cart = false;\n $adding_to_cart = wc_get_product( $product_id );\n\n if ( ! $adding_to_cart ) {\n continue;\n }\n\n $add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );\n\n \/*\n * Sorry.. if you want non-simple products, you're on your own.\n *\n * Related: WooCommerce has set the following methods as private:\n * WC_Form_Handler::add_to_cart_handler_variable(),\n * WC_Form_Handler::add_to_cart_handler_grouped(),\n * WC_Form_Handler::add_to_cart_handler_simple()\n *\n * Why you gotta be like that WooCommerce?\n *\/\n if ( 'simple' !== $add_to_cart_handler ) {\n continue;\n }\n\n \/\/ For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.\n $quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );\n $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );\n\n if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {\n wc_add_to_cart_message( array( $product_id => $quantity ), true );\n }\n}\n}\n\n \/\/ Fire before the WC_Form_Handler::add_to_cart_action callback.\n add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );<\/pre>\n\n\n\nThis functions enables to add mutiple products id in cart url like http:\/\/shop.com\/shop\/cart\/?add-to-cart=3001,3282 <\/p>\n\n\n\n
Then i added the following code in archive-product.php file after “while ( have_posts() ) {“<\/p>\n\n\n\n
if($a){\n $a .= ',';\n }\n the_post();\n$a .= get_the_ID();<\/pre>\n\n\n\nFinally i added the following link code at bottom<\/p>\n\n\n\n
<a href=\"?add-to-cart=<?php echo $a; ?>\">Add all to cart<\/a><\/code><\/pre>\n\n\n\nComment below, if you have any question. <\/p>\n","protected":false},"excerpt":{"rendered":"
Few days ago, i got a projects in upwork that requires a custom function to add all products from woocommerce<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[170,98],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-add-multiple-products-to-cart","tag-woocommerce"],"yoast_head":"\n
How to add all products to cart in woocoomerce? - WordPress Support<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n