{"id":530,"date":"2019-11-13T12:59:11","date_gmt":"2019-11-13T12:59:11","guid":{"rendered":"http:\/\/shaplakanon.com\/wpsupport\/?p=530"},"modified":"2019-11-13T12:59:14","modified_gmt":"2019-11-13T12:59:14","slug":"how-to-add-all-products-to-cart-in-woocoomerce","status":"publish","type":"post","link":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/","title":{"rendered":"How to add all products to cart in woocoomerce?"},"content":{"rendered":"\n

Few days ago, i got a projects in upwork<\/a> that requires a custom function to add all products from woocommerce archive page to cart by clicking a single link or button. Today i going to share how i solved it.<\/p>\n\n\n\n

First i collect the following function from stackoverflow<\/a>.<\/p>\n\n\n\n

function 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\n

This 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\n

Finally 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\n

Comment 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":"\nHow to add all products to cart in woocoomerce? - WordPress Support<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add all products to cart in woocoomerce? - WordPress Support\" \/>\n<meta property=\"og:description\" content=\"Few days ago, i got a projects in upwork that requires a custom function to add all products from woocommerce\" \/>\n<meta property=\"og:url\" content=\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\" \/>\n<meta property=\"og:site_name\" content=\"WordPress Support\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/getwpsupport\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-13T12:59:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-13T12:59:14+00:00\" \/>\n<meta name=\"author\" content=\"Ruhul Amin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ruhul24\" \/>\n<meta name=\"twitter:site\" content=\"@ruhul24\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ruhul Amin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\"},\"author\":{\"name\":\"Ruhul Amin\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/f5f224239283c27426dadc2e4cc9a825\"},\"headline\":\"How to add all products to cart in woocoomerce?\",\"datePublished\":\"2019-11-13T12:59:11+00:00\",\"dateModified\":\"2019-11-13T12:59:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\"},\"wordCount\":105,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#organization\"},\"keywords\":[\"add multiple products to cart\",\"woocommerce\"],\"articleSection\":[\"wordpress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\",\"url\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\",\"name\":\"How to add all products to cart in woocoomerce? - WordPress Support\",\"isPartOf\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#website\"},\"datePublished\":\"2019-11-13T12:59:11+00:00\",\"dateModified\":\"2019-11-13T12:59:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/shaplakanon.com\/wpsupport\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add all products to cart in woocoomerce?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#website\",\"url\":\"https:\/\/shaplakanon.com\/wpsupport\/\",\"name\":\"WordPress Support\",\"description\":\"Wordpress Repair, migration, speed optimization\",\"publisher\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/shaplakanon.com\/wpsupport\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#organization\",\"name\":\"WordPress Support\",\"url\":\"https:\/\/shaplakanon.com\/wpsupport\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/shaplakanon.com\/wpsupport\/wp-content\/uploads\/sites\/2\/2022\/05\/cropped-logo-9674824.png\",\"contentUrl\":\"https:\/\/shaplakanon.com\/wpsupport\/wp-content\/uploads\/sites\/2\/2022\/05\/cropped-logo-9674824.png\",\"width\":274,\"height\":55,\"caption\":\"WordPress Support\"},\"image\":{\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/getwpsupport\/\",\"https:\/\/x.com\/ruhul24\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/f5f224239283c27426dadc2e4cc9a825\",\"name\":\"Ruhul Amin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28178d347289d520b52ece250f7042df?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28178d347289d520b52ece250f7042df?s=96&d=mm&r=g\",\"caption\":\"Ruhul Amin\"},\"sameAs\":[\"https:\/\/shaplakanon.com\/wdc\/\"],\"url\":\"https:\/\/shaplakanon.com\/wpsupport\/author\/sabahat\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add all products to cart in woocoomerce? - WordPress Support","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/","og_locale":"en_US","og_type":"article","og_title":"How to add all products to cart in woocoomerce? - WordPress Support","og_description":"Few days ago, i got a projects in upwork that requires a custom function to add all products from woocommerce","og_url":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/","og_site_name":"WordPress Support","article_publisher":"https:\/\/facebook.com\/getwpsupport\/","article_published_time":"2019-11-13T12:59:11+00:00","article_modified_time":"2019-11-13T12:59:14+00:00","author":"Ruhul Amin","twitter_card":"summary_large_image","twitter_creator":"@ruhul24","twitter_site":"@ruhul24","twitter_misc":{"Written by":"Ruhul Amin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#article","isPartOf":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/"},"author":{"name":"Ruhul Amin","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/f5f224239283c27426dadc2e4cc9a825"},"headline":"How to add all products to cart in woocoomerce?","datePublished":"2019-11-13T12:59:11+00:00","dateModified":"2019-11-13T12:59:14+00:00","mainEntityOfPage":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/"},"wordCount":105,"commentCount":0,"publisher":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#organization"},"keywords":["add multiple products to cart","woocommerce"],"articleSection":["wordpress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/","url":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/","name":"How to add all products to cart in woocoomerce? - WordPress Support","isPartOf":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#website"},"datePublished":"2019-11-13T12:59:11+00:00","dateModified":"2019-11-13T12:59:14+00:00","breadcrumb":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/shaplakanon.com\/wpsupport\/how-to-add-all-products-to-cart-in-woocoomerce\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/shaplakanon.com\/wpsupport\/"},{"@type":"ListItem","position":2,"name":"How to add all products to cart in woocoomerce?"}]},{"@type":"WebSite","@id":"https:\/\/shaplakanon.com\/wpsupport\/#website","url":"https:\/\/shaplakanon.com\/wpsupport\/","name":"WordPress Support","description":"Wordpress Repair, migration, speed optimization","publisher":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/shaplakanon.com\/wpsupport\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/shaplakanon.com\/wpsupport\/#organization","name":"WordPress Support","url":"https:\/\/shaplakanon.com\/wpsupport\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/logo\/image\/","url":"https:\/\/shaplakanon.com\/wpsupport\/wp-content\/uploads\/sites\/2\/2022\/05\/cropped-logo-9674824.png","contentUrl":"https:\/\/shaplakanon.com\/wpsupport\/wp-content\/uploads\/sites\/2\/2022\/05\/cropped-logo-9674824.png","width":274,"height":55,"caption":"WordPress Support"},"image":{"@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/getwpsupport\/","https:\/\/x.com\/ruhul24"]},{"@type":"Person","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/f5f224239283c27426dadc2e4cc9a825","name":"Ruhul Amin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/shaplakanon.com\/wpsupport\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28178d347289d520b52ece250f7042df?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28178d347289d520b52ece250f7042df?s=96&d=mm&r=g","caption":"Ruhul Amin"},"sameAs":["https:\/\/shaplakanon.com\/wdc\/"],"url":"https:\/\/shaplakanon.com\/wpsupport\/author\/sabahat\/"}]}},"_links":{"self":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":0,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"wp:attachment":[{"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shaplakanon.com\/wpsupport\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}