Home > Tips & Tricks > WooCommerce Automatically Add Product to Cart on Product Page Visit

WooCommerce Automatically Add Product to Cart on Product Page Visit

If you want to add woocommerce product to the cart automatically when you visit the product single page then you are at the right place.

In this trick, I will show you the way to add a product to the cart automatically. It is very simple, you just have to make the action hook function that will add a product to the cart based on condition. See the following action hook function for reference.

WooCommerce Automatically Add Product to Cart on Product Page Visit

Add the above code in your child theme’s functions.php file and save it. Now you will see your product automatically add to the cart when you visit any specific product single page.

In the above code, first I checked that the user is not admin and on a single product page to run the further condition. Then I get the product ID from the global $product variable and then check the cart items using the $woocommerce variable. If the same product already exists in the cart then it will not add the same product to the cart, also checked if no products on the cart then also add the item to the cart.

Note: You can customize the above code based on your requirements like if you want to add the product to the cart on specific conditions and specific products, IP-based, price-based, etc.

Being Tricky 😉

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment