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?
- Promotional Offers: Automatically include a promotional item in the cart to boost customer satisfaction and increase sales.
- Subscription Services: Automatically add a trial product or the first subscription item for new visitors.
- 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.
- Create a Child Theme (if you don’t already have one):
- Visit our Free WordPress Child Theme Generator to create and download your child theme.
- Follow the instructions to install and activate your child theme.
- Add Code to
functions.php:- Navigate to Appearance > Theme File Editor in your WordPress dashboard.
- Select the
functions.phpfile 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.
Use Cases for Automatically Adding Products to Cart
- Free Samples: Automatically include a free sample with every visit to encourage customers to try new products.
- Limited-Time Offers: Add promotional items during sales events to ensure customers don’t miss out.
- Welcome Gifts: Provide a welcome gift to new customers to enhance their first shopping experience.
- 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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

