• Resolved calduchoweb

    (@calduchoweb)


    Hello,
    I need to create a URL that directly adds multiple products to the cart. I have found that you can add a product by adding ?add-to-cart=ID but I need to add several different products, that is to say several different IDs ?add-to-cart=ID1+ID2.

    Could you tell me the correct way to create that URL?

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey @calduchoweb,

    Out of the box, this isn’t possible. Only one product can be added to the cart at a time with the add-to-cart URL feature.

    There is a Stack Overflow post that shows how some users have made this work for them.

    https://stackoverflow.com/questions/42570982/adding-multiple-items-to-woocommerce-cart-at-once

    I’d take a look at that and see if it will work for you. It appears this answer is more up to date.

    Hopefully, that’ll get you pointed in the right direction.

    Cheers

    Thread Starter calduchoweb

    (@calduchoweb)

    Hello @3sonsdevelopment

    Many thanks! It is just what I was looking for.
    In case someone else helps, the code that has worked for me is the following:

    add_action( 'wp_loaded', 'add_multiple_to_cart_action', 20 );
    
    function add_multiple_to_cart_action() {
        if ( ! isset( $_REQUEST['multiple-item-to-cart'] ) || false === strpos( wp_unslash( $_REQUEST['multiple-item-to-cart'] ), '|' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
                return;
            }
    
        wc_nocache_headers();
    
        $product_ids        = apply_filters( 'woocommerce_add_to_cart_product_id', wp_unslash( $_REQUEST['multiple-item-to-cart'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
        $product_ids = explode( '|', $product_ids );
        if( ! is_array( $product_ids ) ) return;
    
        $product_ids = array_map( 'absint', $product_ids );
        $was_added_to_cart = false;
        $last_product_id = end($product_ids);
        //stop re-direction
        add_filter( 'woocommerce_add_to_cart_redirect', '__return_false' );
        foreach ($product_ids as $index => $product_id ) {
            $product_id = absint(  $product_id  );
            if( empty( $product_id ) ) continue;
            $_REQUEST['add-to-cart'] = $product_id;
            if( $product_id === $last_product_id ) {
    
                add_filter( 'option_woocommerce_cart_redirect_after_add', function() { 
                    return 'yes'; 
                } );
            } else {
                add_filter( 'option_woocommerce_cart_redirect_after_add', function() { 
                    return 'no'; 
                } );
            }
    
            WC_Form_Handler::add_to_cart_action();
        }
    }

    A greeting!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘URL to add to cart multiple products’ is closed to new replies.