Giesburts
Giesburts

Reputation: 7668

Automatically add multiple products to cart on visit WooCommerce

I am working on a webshop test case and what I would like is to add automatically items/products to the cart on visit. So I searched for something like that when I found the same code over and over everywhere.

/*
 * goes in theme functions.php or a custom plugin
 **/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 64;
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
} 

From:

https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/ https://gist.github.com/kloon/2376300

So this works fine if you have only one product but I would like to add more than just 1 product. Is there someone with some PHP knowledge (and a some WordPress) that can help me out? Thanks in advance!

Upvotes: 1

Views: 2160

Answers (1)

Tosfera
Tosfera

Reputation: 522

There are actually two ways you can do this, at least. You can either call the add_to_cart function more than once, or create a loop. Both ways are down here:

Method 1

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 64;
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
            WC()->cart->add_to_cart( $product_id2 );
            WC()->cart->add_to_cart( $product_id3 );
            WC()->cart->add_to_cart( $product_id4 );
        }
    }
}

Method 2

foreach ($articles as $article) {
    WC()->cart->add_to_cart( $article );
}

Do note that you should create a new array called articles holding all the IDs from the desired products. Another thing that is going to bother you then is checking whether the cart holds more than 0 items, and checking if all of them are in there.

Method 2 would look something like this:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $articles = array(64);
        $found = false;

        // check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];

                if (($key = array_search($_product->id, $articles)) !== false)
                    unset($articles[$key]);
            }

            // if product not found, add it
            if ( count($articles) > 0 ) {
                foreach ($articles as $article) {
                    WC()->cart->add_to_cart($article);
                }
            }
        } else {
            // if no products in cart, add it
            foreach ($articles as $article) {
                WC()->cart->add_to_cart( $article );
            }
        }
    }
}

Upvotes: 2

Related Questions