• Resolved clingendael

    (@clingendael)


    Hi all,

    I am working on a schoolproject. For my project I need to create a webshop. I am using Woocommerce for my project and I like it a lot.

    At the moment I am stuck at adding multiple products to the cart at once. Is there a way to make this possible. Maybe by adding a checkbox next to the products and a button to add all selected products to the cart. Or another way if this is possible.

    Thanks!

    • This topic was modified 6 years, 11 months ago by clingendael.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Please add function.php

    function woocommerce_maybe_add_multiple_products_to_cart() {
    // 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 $product_id ) {
        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();
        }
    
        $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, you're on your own.
         *
         * Related: WooCommerce has set the following methods as private:
         * WC_Form_Handler::add_to_cart_handler_variable(),
         * WC_Form_Handler::add_to_cart_handler_grouped(),
         * WC_Form_Handler::add_to_cart_handler_simple()
         *
         * Why you gotta be like that WooCommerce?
         */
        if ( 'simple' !== $add_to_cart_handler ) {
            continue;
        }
    
        // For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
        $quantity          = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
        $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',        'woocommerce_maybe_add_multiple_products_to_cart', 15 );
    Thread Starter clingendael

    (@clingendael)

    Hi Logicrays,

    My site broke after adding this. Do you know why by any chance?

    Thanks!

    Thread Starter clingendael

    (@clingendael)

    Hi Logicrays,

    Thanks for your fast reply! Unfortunately it does not work.

    dougaitken

    (@dougaitken)

    Automattic Happiness Engineer

    Hi @clingendael

    Did you get a solution for this? What is the general set up that you’re looking to achieve with this?

    @logicrays – posting random snippets to functions.php is never a good idea unless the person knows what is happening.
    I’ve seen you’ve contributed a few times to the forums saying very similar “put this in your functions.php file.
    Please do help out but if offering a custom code solution, either use the code syntax or link to a Gist or similar service and comment about what is happening in the code.

    Thanks,

    Thread Starter clingendael

    (@clingendael)

    Hi Doug,

    Thanks for your reply!

    Unfortunately I am still looking for a solution. The general set up that I am looking for is to select multiple products in my store and adding all selected to the cart at once. Maybe with a checkbox next to the item’s name or something with a similar kind of idea.

    I was wondering if this is possible with Woocommerce.

    Here is a link to my schoolproject:
    https://sportboards.nu/winkel/

    Thanks.

    dougaitken

    (@dougaitken)

    Automattic Happiness Engineer

    Hey @clingendael

    Maybe with a checkbox next to the item’s name or something with a similar kind of idea.

    Ah! So we have a page on our docs site which might be of help to you – How to Bundle Products.

    If you are looking for a more generic option or customized, then I’d recommend reaching out to one of the services on our Customizations page (https://woocommerce.com/customizations/).

    Thanks,

    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Adding multiple products to cart at once’ is closed to new replies.