PHPWooCommerceWordPress

How to create Add multiple products in a cart in a single click in WordPress

0
1.4k
2018 / 08 / 09

For multiple products:

function woocommerce_maybe_add_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 );
        if ( 'simple' !== $add_to_cart_handler ) {
            continue;
        }
        $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 );
        }
    }
}
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );

$product_ids = implode( ',', array( 1, 2, 55 ) );
$url = esc_url_raw( add_query_arg( 'add-to-cart', $product_ids, wc_get_checkout_url() ) );


// create add to cart button for multiple product ID
function rec_multiple_add_to_cart_button($atts) {
    $atts_var = shortcode_atts( array(
        'id' =>  '',
        'title' =>  ''
    ), $atts );
    
    $product_ids = esc_attr($atts_var['id']);
    $title = esc_attr($atts_var['title']);

    $url = esc_url_raw( add_query_arg( 'add-to-cart', $product_ids, wc_get_checkout_url() ) );

    $button = '<a href="'.$url.'" class="single_add_to_cart_button button alt">'.$title.'</a>';
    return $button;
}
add_shortcode( 'rec_multi_prod_cart', 'rec_multiple_add_to_cart_button' );

The shortcode will be [rec_multi_prod_cart id=”11,22,33″ title=”Add to Cart”], where 11, 22 and 33 are the product ID and title will be displayed as button text and the shortcode will generate a simple button which will do add to cart for that particular product. You can call this shortcode anywhere you want. It can be widget or content of page or post.

 

If you wish to add sinlgle product to cart in a single click you can check article here.

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks