In the previous post, I shared with you how to create WooCommerce product programmatically. Now, If you want to add product to cart programmatically in WooCommerce, you can achieve this by adding an action hook and a callback function with the logic to add the product to cart programmatically. Ideally, you want to add the product to cart after a certain even occurs and trigger the automatic add to cart.
WooCommerce Add Product to Cart Programmatically
There are several events you can hook on to before you add the product to cart programmatically depending on what you want to achieve. One of the most common hooks that you can use is the
template_redirect hook that is fired before determining the template to be loaded.
If you are looking for example on how to add product to cart programmatically, I will illustrated in this quick tutorial and share the code snippet that you can add to the functions.php
or your plugin files to help with adding the product to cart automatically.
Template Redirect Hook Example
As I have mentioned above, you need to determine when you want the product to be added to the cart automatically based on what you want to achieve in your logic.
A good example is the product can be automatically added to cart when there is a page redirect. This can be achieved using the template redirect action hook as in the code below :
add_action( 'template_redirect', 'njengah_add_to_cart_programmatically');
Code to Add Product to Cart Automatically
Now we need to add the code that will check if the cart is empty, then add the specific product to cart and possibly redirect to checkout or any other page we may want the user to be redirected to after the product is automatically added to cart.
The following is the code snippet that checks the cart to see its empty then added the product we want ( in this case you should replace the Product_Id) with the actual product ID for the product we want to add to cart automatically.
WC()->cart->empty_cart(); // Check if cart is empty WC()->cart->add_to_cart( Product_ID ); // this adds the product with the ID; we can also add a second variable which will be the variation ID wp_safe_redirect( wc_get_checkout_url() ); // redirects to the checkout page exit(); // safely closes the function
As you can see from the code above, when the product is added to cart, we can redirect an another page like in this case we are redirecting to checkout page after we added the product to cart.
Complete Code Snippet to Add Product to Cart Programmatically
For the code above to work, we need to wrap it in the call back function of the action hook we created in the first step so that the product will be automatically added to cart during the template_redirect event.
In this case the complete code including the action hook and the callback function with the code to add product to cart should be as follows :
//Action Hook add_action( 'template_redirect', 'njengah_add_to_cart_programmatically'); //Callback Function function njengah_add_to_cart_programmatically(){ WC()->cart->empty_cart(); // Check if cart is empty WC()->cart->add_to_cart( Product_ID ); // this adds the product with the ID; we can also add a second variable which will be the variation ID wp_safe_redirect( wc_get_checkout_url() ); // redirects to the checkout page exit(); // safely closes the function }
Conclusion
In this post, we have looked at how you can automatically add product to cart during specific WooCommerce or WordPress events. This can be varied by changing the action hook and can also be extended to suit a wide range of uses in your custom WooCommerce theme or plugin. If you would like this code added to your project and have no idea how to get this done, you can feel free to get in touch with me for further help.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Hide Product Image on Cart Page WooCommerce
- How to Remove Cart Page WooCommerce Storefront Theme
- How to Fix Uncaught TypeError: wp.template is not a function
- How to Create My Account Page In WooCommerce
- How to Get Order Details After Checkout In WooCommerce
- How to Hide, Change or Remove WooCommerce On Sale Badge
- How to Hide Product Description Heading WooCommerce
- How to Move Description Under Image in WooCommerce
- WooCommerce Login Redirect Hook Explained with Example
- How to Change Product Price Programmatically in WooCommerce
- How to Create Order Programmatically WooCommerce
- How to Create Product Programmatically WooCommerce
- How to Hide Price If Zero In WooCommerce
- How to Change Checkout Endpoints WooCommerce
- How to Clear Cart on Logout In WooCommerce
- Complete Guide to Using WordPress Custom Hooks do_action & apply_filters With Examples
- How to Hide WooCommerce Marketing Hub Admin Option
- How to Redirect Users after Successful Login in WordPress without Using a Plugin