Add Product to Cart Programmatically
Usually when I work with any WordPress or WooCommerce projects, if I face with something new during the development process, I usually try to share what I learnt immediately on my blog, here. But not always I did that. In 2014 I began my work with the awesome and super complicated project with WooCoommerce, WooCommerce Memberships and WooCommerce Subscriptions. A lot of custom code was created and now I want to share with you the whole process, line by line. Something could seems simple for you, somthing – not. Anyway let’s go through it together ⚡️
Add Product to the Cart
Let’s begin with something simple, as always. First of all let me show you how to add a product with a specific ID to the cart.
Please keep in mind that if this specific product is already in the cart, its quantity will be increased by 1.
global $woocommerce; $woocommerce->cart->add_to_cart( $product_id ); // you can pass a number here
You can also do it another way without global $woocommerce
:
WC()->cart->add_to_cart( $product_id );
To keep the things simple I am going to use the second one during this whole tutorial. But you can use any way you like the most.
Add Product to Cart with Custom Quantity
Do not expect something tricky here, all we have to do is to add one more parameter to the function which is going to present the quantity of a product added to the cart.
WC()->cart->add_to_cart( $product_id, $quantity );
Okay, let’s suppose that we want to add five products with ID 500. How our code is going to look like?
Here is how:
WC()->cart->add_to_cart( 500, 5 );
Add Product Variation to Cart Programmatically
If you want to add a product variation to the cart, you have to know not only a product ID but also a product vaiation ID. Both of them should be passed to add_to_cart()
method.
Example:
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
How to Check if a Specific Product is Already in the Cart?
As you probably remember, I already mentioned, that if we are using add_to_cart()
method for the same product twice, the product quantity in the cart will be increased.
But what if we do not want to increase the product quantity?! 🤔
The most logical way seems to check if a certain product is already in the cart before using add_to_cart()
. Ok, let’s do it! I am going to use method find_product_in_cart()
.
if( WC()->cart->find_product_in_cart( $product_cart_id ) ){ }
Here we must have an understanding of a difference between $product_id
and $product_cart_id
. When a certain product is in the cart it also has an ID which represents it in the cart. And this ID is different from its product ID. How to get the product cart ID? With generate_cart_id()
method of course 😁 Let me just show you the ready to use code!
$product_id = 55; $product_cart_id = WC()->cart->generate_cart_id( $product_id ); if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){ // Yep, the product with ID 55 is NOT in the cart, let's add it then! WC()->cart->add_to_cart( $product_id ); }
Not so difficult, right?
There is also another option – to empty the cart every time before using add_to_cart()
. Like this:
WC()->cart->empty_cart(); WC()->cart->add_to_cart( $product_id );
In some cases in can be a better decision.
Automatically Add a Product to the Cart on a Custom Page
The idea is to add a specific product to customer’s cart when he/she visits a certain page on your website. It can be easily achieved inside template_redirect
action hook.
add_action( 'template_redirect', 'misha_add_to_cart_on_custom_page'); function misha_add_to_cart_on_custom_page(){ if( is_page( 'my-page' ) ) { // "my-page" is a page slug WC()->cart->add_to_cart( 72 ); // add to cart product with ID 72 } }
- This code will work out not only on
template_redirect
action hook, but I recommend to use it. - On line 5 we used a conditional tag
is_page()
, it accepts not only a page slug, but also a page ID, or an array of IDs / slugs - There are also many other conditional tags like
is_front_page()
,is_category()
,is_search()
etc, you can read more about them in official WordPress Codex here.
Add to Cart and then Redirect to Checkout
I have a kind of similar tutorial where I describe how to automatically send users to checkout when a product has been added to the cart. But now we add products to the cart in code and the methods described there doesn’t apply for this case.
Let’s just modify our previous piece of code slightly.
add_action( 'template_redirect', 'misha_add_to_cart_on_custom_page_and_redirect'); function misha_add_to_cart_on_custom_page_and_redirect(){ if( is_page( 'my-page' ) ) { // you can also pass a page ID instead of a slug WC()->cart->add_to_cart( 72 ); // add to cart product with ID 72 wp_safe_redirect( wc_get_checkout_url() ); exit(); } }
- If you want to redirect to external domain, use
wp_redirect()
instead ofwp_safe_redirect()
or extend the list of safe domains withallowed_redirect_hosts
filter.
Comments — 4
Hi Misha,
Thank you for this article.
I have a WoocCommerce shopping that my product gets from another site API.
Is there a way to add an item to my cart without being on the Woocommers product list?
Hi,
Yes, you can still use
WC()->cart->add_to_cart( $product_id )
function accept only product ID.
I want to add a product that not exists in Woocommerce Product List for example:
wp_insert_post()
should help in this case.Comments are closed.