WooCommerce add multiple products to cart using URL

WooCommerce add multiple products to cart using URL

WooCommerce provides a builtin shortcode for add to cart product, by default the shortcode works only for a simple product. But what if you want to add multiple products to cart using URL in WooCommerce. However, Using shortcode you can’t able to add multiple products Ids. WooCommerce provide add_to_cart and add_to_cart_url shortcodes for simple products add to cart.

Let’s see how you can use the add_to_cart and add_to_cart_url shortcodes with output.

WooCommerce Products add_to_cart and add_to_cart_url

WooCommerce add_to_cart and add_to_cart_url both shortcodes are works same. The add_to_cart shortcode generate add to cart button and add_to_cart_url shortcode return URL link for a specified function like.

[add_to_cart id="1"]
[add_to_cart_url id="1"]

Both shortcodes generate the same URL to add to cart product like.

https://tryvary.com/cart/?add-to-cart=1

When you run this URL it will add the product to cart.

How to find the product ID?

You will find the product ID from the WordPress admin panel product listing page. You can just place your cursor at the product line and it will show the product ID like.

WooCommerce add multiple products to cart using url

Finally, We are going to learn WooCommerce add multiple products to cart using the same URL with multiple products Ids.

WooCommerce add multiple products to cart using URL

Finally, We learn how to use shortcode to add to cart product. Now, We are going to see how to add multiple products to the cart. First of all, You should open the functions.php file of your currently active theme. Also, You can add this code inside your plugin file if you want to use it from the plugin.

After, Opening file simply past the code inside the file.

function tryvary_woocommerce_allow_multiple_products_to_cart() {
    if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
        return;
    }

    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 ) {
            $_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. It's works only for simple products.
         */
        if ( 'simple' !== $add_to_cart_handler ) {
            continue;
        }

        $quantity = 1;
        $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', 'tryvary_woocommerce_allow_multiple_products_to_cart', 15 );

Finally, You are ready to add multiple products to cart using URL like.

https://tryvary.com/cart/?add-to-cart=1,2,5

See, The shortcode still works with a single product ID. You can able to pass the multiple products Ids using a shortcode. In Other Words, You must create add to cart URL manually with multiple product IDs.

In conclusion, Today, We learn how to add multiple products at a time. It’s useful when you want to sell combo items in WooCommerce. I hope this article is helpful to you. If you have any question please write the comment. We are always ready to help you.

Related Posts

  1. How to Remove WooCommerce tabs from product page
  2. Add to cart variable products WooCommerce with variation button URL
  3. WordPress do_shortcode in template PHP file
  4. WordPress Button Shortcode – Add button to WordPress editor
  5. WordPress shortcode – How to create shortcode in WordPress
  6. WordPress change URL in database using MySQL Query
  7. WordPress jQuery is Not Defined or undefined
  8. Enable shortcodes in widget WordPress
  9. How to use jQuery noConflict mode in WordPress
  10. WooCommerce add product to cart with custom price

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Posts

Subscribe To Newsletter

Get Early Access To New Articles, Plugins, Discount Codes And Quickly Brief Updates about tips and tricks

Join 357 other subscribers

%d bloggers like this: