I am working on a webshop test case and what I would like is to add automatically items/products to the card 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 );



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!

Read more here: Automatically add products to cart on visit (more than 1 product!) WooCommerce


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: