WC()->cart->add_to_cart() not working when cart page is loaded directly?

I want to add a product automatically to the cart, whenever the user goes to the cart page.

I have added the following code in cart.php:

WC()->cart->add_to_cart( $product_id );

When I try to simply visit the cart page by entering the url - example.com/cart/ , then the product above does not get added, however, when I add another product to cart on live site and click 'view cart', this particular product that I have added above via code, also gets added to the cart (I tried both, as logged in user and as guest).

I did try to set session cookie on before the above code but that didn't work either:

WC()->session->set_customer_session_cookie( true );

Solutions

Try the following:

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
//check if we are on cart page
    if (is_cart()) {
        $product_id = 69; //replace with your own product id
        $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->get_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 );
        }
    }
}

Similar questions

Query vars in url (wordpress) work fine when page loaded directly, but fail on ajax
I've a little issue, and I couldn't find anything about it... I'm in deadlock. So I've wp page that contains normal loop, if I call this file directly in browser all works fine, and I can even use query-vars such as: http://domain.com/blog/wp-content/themes/wovies-bones/getmovies.php?actor=x And it works fine, I get single post that has custom taxo...
Make WC_Cart add_to_cart method working for guests in Woocommerce
Im trying to get my code to work. Searching for hours now. I found similar questions like this one. But unfortunately noone seems to find a solution. In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on check...
Wordpress: query_posts in a page that gets loaded into a div after the page is loaded
I just got jQuery to be able to load a page into a body div. Now I'm trying to get wordpress functionality into various pages that I can then load into that body div. Everything loads and I know the right pages are being accessed but I've run into a very unexpected problem. Fatal error: Call to undefined function wp_head() in /home/nighthav/public_...
WooCommerce add_to_cart metadata not sticking
I am a WP noob but very comfortable in PHP. I am working with a client and we have built a product customization tool as an Angular.js single page application. When the product is finished being customized we are seeking to inject it into a WooCommerce cart so the client can check out. To do this we are $_POSTing the data to a PHP file in the root ...
WooCommerce hook "add_to_cart" does not send quantity
I'm subscribing to a woocommerce hook and it's supposed to include the $quantity argument in the callback function but it looks like it's never sent. Quantity is always one and if I print out the arguments, it shows that the function has only one argument which is the first one ($cart_item_key). Am I missing something? The woocommerce_add_to_cart e...
Make modification of add_to_cart button specific to single page
I am using WooCommerce for the eCommerce part of my WordPress site. When using the add_to_cart button I want it to say "read more" on a specific webpage only. So far I have found the code to change the button text: The question is, how do I make this apply to the appearance of the add_to_cart button on a single page of my website? The page is not a...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.