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 😉

Leave a Reply

Your email address will not be published.