Automatically add products to cart on visit

Remi | December 7, 2013 | in WooCommerce

Adding products to cart automatically when a visitor is on your site is something that can be very useful when you use WooCommerce. It could what ever you want: an insurance, a sale product etc… And guess what? This is is pretty easy to do. All you need is a little snippet to add to functions.php, in your theme folder. To choose the product to add to the cart, simply find its ID, and then replace the default value in the snippet below.

You probably saw this commercial method on many websites, and i have to say that it’s pretty powerful, but be careful when using it because it annoy a bunch of people. If you think like a visitor, you add a product to your cart and when you arrive on the cart page, there are … two products in it. Well, there are pros and cons but depending on your business it can good or bad. Let’s say you sell mobile phones, then your customers would may be interested in purchasing an insurance for their mobiles.

But if you sell hoodies, and you automatically add socks, shoes or whatever that isn’t really related to the first product, then i’m not sure your had the best idea to add this product to the cart automatically. Hopefully, clients can remove the product from the cart, but it’s an extra action that you ask them to do, and the more actions you ask you visitors to do, the less they will be keen on finishing the checkout process.

So, here is the snippet. Place it in functions.php in your theme folder:

Then, what i would do is probably to check products already in cart and then automatically add a related product to the cart, that would make things a bit more efficient. And finally, i would totally avoid adding a random product to cart.

The author

About the author Remi: Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!


  1. apalmqu

    Hey Remi,
    Is there any way to do something similar to this, but rather when a customer adds a 1st product (‘Product A’) to their cart THEN it would automatically add the 2nd product (“Product B’) to the cart?

    I realize this could be very undesirable for most companies, but for our products it makes complete sense. Preferably, Product B, the “auto” product, would be free but would only be automatically added for a specific few products (like when adding Product A). Because it’s a free, auto product (BTW – shipping would still need to apply) it would be nice if it was hidden from the catalog so it can’t be added as a regular product. Does that make sense?

    P.S. – What would make that absolutely PERFECT for our needs is if the customer could choose separate shipping methods for both Product A and Product B. I cannot find anything remotely close to that! Almost everything we sell needs to be split up into 2 shipments, but the first shipment is less important (therefore not needed as quick) than the second. Is it possible to basically have 2 shipping rates in one checkout? For example, if the customer wants Product A shipped Express, but wants the free Product B shipped Standard.

  2. Pingback: Blogging for Benjamin Update | Daniel Espinoza

  3. scoophk

    This is working nicely as a plugin – thank you – but…
    The item I am auto-adding to the cart is a safety accessory – as some users may have it already they would have to be able to remove it from the cart if they wish to.

    Right now it keeps coming back – if it can’t be deleted is there a way to set the number purchasing to ‘0’ initially?

    Thank you!!

  4. Patrick

    Just need to point out that if you have an online shop anywhere in the EU, or (I believe) if it sells to customers in the EU then you can’t do this.

    A new Europe wide law governing online sales, the EU Consumer Rights Directive, specifically outlaws automatically adding paid for products to a customer’s shopping cart.

    Also, (off topic, I know) the Place Order text on WooCommerce checkout is not good enough to satisfy the CRD – it needs to be different depending on what you are selling – so I’ve suggested to WooCommerce that they make it easier to edit via the bank end, but in the meantime, do you have a snippet you could share to change the Place Order button text?


  5. mfthomas

    Hi good day,

    I have tried the code above.

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

    But it is not working on my end. Please see code below.

    function process_product_additional_data($data){
    global $woocommerce;
    $product_id = 334;
    $quantity = 1;
    $variation_id = ”;
    $variation = array();
    $cart_item_data = array();

    $woocommerce->cart->add_to_cart($product_id,$quantity, $variation_id, $variation, $cart_item_data);

    Hope you can help me with this.


  6. mfthomas

    Hi Remi,

    Sorry I was not able to include all the code that I am working on, kindly see below.

    I am trying to use contact form 7 as a booking form on a wordpress page and when submitted it will add the product and item data from cf7 form to cart and then redirect to checkout.

    Not sure if it can be done.

    /* Contact Us Form */
    // txtService = product dropwdown
    // txtName = client name
    // etc. other fields

    /*Override cf7 send mail*/
    add_action("wpcf7_before_send_mail", "process_form_override_for_booking");
    function process_form_override_for_booking($wpcf7_data) {
    $wpcf7 = WPCF7_ContactForm::get_current();

    // get current SUBMISSION instance
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
    // get submission data
    $data = $submission->get_posted_data();

    $wpcf7->skip_mail = true;

    //Process cf7 data to product
    function process_product_additional_data($data){
    global $woocommerce;
    $product_id = ''; // assign product id from cf7
    $quantity = 1;
    $variation_id = '';
    $variation = array();
    $cart_item_data = array(); // assign data from cf7

    //assign to be completed
    $product_id = $data['txtService'];

    //add product to cart with product item data
    $woocommerce->cart->add_to_cart($product_id,$quantity, $variation_id, $variation, $cart_item_data);

    Hope to hearing your advice.

Leave a reply