How to Programmatically Add Product to Cart in WooCommerce on Visit – 2026

Last updated on August 2nd, 2024 at 12:34 pm

Automating certain processes in your WooCommerce store can streamline the shopping experience for your customers and boost sales. One such process is adding a product to the cart automatically when a customer visits your site. This guide will show you how to add a product to the cart programmatically in WooCommerce upon a visit, without using any plugins.

Why Automatically Add Products to Cart?

  1. Promotional Offers: Automatically include a promotional item in the cart to boost customer satisfaction and increase sales.
  2. Subscription Services: Automatically add a trial product or the first subscription item for new visitors.
  3. Special Campaigns: Use this tactic during special events or sales to ensure customers don’t miss out on featured items.

Step 1: Adding Code to Your Theme’s Functions File

To add a product to the cart programmatically, you’ll need to insert custom code into your theme’s functions.php file. If you don’t already have a child theme, create one to ensure your changes are not lost when the theme updates. You can create a child theme using our free child theme generator.

  1. Create a Child Theme (if you don’t already have one):
  2. Add Code to functions.php:
    • Navigate to Appearance > Theme File Editor in your WordPress dashboard.
    • Select the functions.php file of your child theme.
    • Add the following code snippet to automatically add a product to the cart upon a visit:
/*
 * Snippet: How to Programmatically Add Product to Cart in WooCommerce on Visit – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1348
* Tested with WooCommerce 10.7.0
* "Add product to cart on visit"
*/ function wcsuccess_add_product_to_cart_on_visit() { if ( is_user_logged_in() && is_shop() ) { $product_id = 123; // Replace 123 with the ID of the product you want to add $found = false; // Check if product is already in cart foreach ( WC()->cart->get_cart() as $cart_item ) { if ( $cart_item['product_id'] == $product_id ) { $found = true; break; } } // If product not found, add it to the cart if ( ! $found ) { WC()->cart->add_to_cart( $product_id ); } } } add_action( 'template_redirect', 'wcsuccess_add_product_to_cart_on_visit' );

Step 2: Test the Functionality

Visit your WooCommerce shop page while logged in to see if the product is automatically added to your cart. Make sure the product ID used in the code matches the product you want to add. You can find the product ID in the WooCommerce product settings.

See also  How to Add a Second Product Image to WooCommerce Shop Pages - 2026

Use Cases for Automatically Adding Products to Cart

  1. Free Samples: Automatically include a free sample with every visit to encourage customers to try new products.
  2. Limited-Time Offers: Add promotional items during sales events to ensure customers don’t miss out.
  3. Welcome Gifts: Provide a welcome gift to new customers to enhance their first shopping experience.
  4. Subscription Trials: Add a trial product automatically to introduce customers to subscription services.

Conclusion

Automatically adding a product to the cart upon a visit can enhance the user experience and boost sales on your WooCommerce store. By implementing the provided code, you can achieve this functionality without the need for additional plugins, ensuring a smooth and personalised shopping experience for your customers.

See also  How to Add Custom User Roles in WooCommerce - 2026

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×