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 WooCommerce, WooCommerce Memberships and WooCommerce Subscriptions. Let me share with you a piece of that work.

In this tutorial I am going to show you a different ways of adding a product to WooCommerce cart via code.

And by the way, if you were looking how to add product to an order, you need a completely different tutorial – here it is.

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 item is already in the cart, its quantity will be increased by 1.

WC()->cart->add_to_cart( $product_id );  // you can also pass a number here

You can also do it another way with global variable $woocommerce:

global $woocommerce;
$woocommerce->cart->add_to_cart( $product_id );

To keep the things simple I am going to use the first one during this whole tutorial. But you can use any way you like the most.

Add Product to Cart with Custom Quantity

I hope you’re not expecting 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.

Nothing tricky here as well, all we have to do is to add one more parameter to the method what allows to set product quantity programmatically.

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.


WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );

Add Product to Cart with Custom Price

One of the ways of doing that is in two steps. First of all we need to pass a custom price of a product as cart item data in WC()->cart->add_to_cart() method. Like this.

WC()->cart->add_to_cart( 14, 1, 0, array(), array( 'misha_custom_price' => 1000 ) );

Once you did that, fire woocommerce_before_calculate_totals action hook and refresh a new custom price there.

add_action( 'woocommerce_before_calculate_totals', 'rudr_custom_price_refresh' );

function rudr_custom_price_refresh( $cart_object ) {

	foreach ( $cart_object->get_cart() as $item ) {

		if( array_key_exists( 'misha_custom_price', $item ) ) {
			$item[ 'data' ]->set_price( $item[ 'misha_custom_price' ] );

A little bit more about overriding product prices in the cart.

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->add_to_cart( $product_id );

In some cases in could 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() );
  • If you want to redirect to external domain, use wp_redirect() instead of wp_safe_redirect() or extend the list of safe domains with allowed_redirect_hosts filter.

Add Items to Cart using JavaScript (AJAX)

The last but not least, I wanted to show you how do the similar things in JS. Sometimes you may need it.

In order to use the function below, add-to-cart.min.js should be enqueued to the page you are going to use the code on. We will definitely check it inside the function as well, but I think it is worth mentioning so when nothing is working you will know why.

You could also notice jQuery. Stop hate please! WooCommerce uses it anyway, so there is no point to remove it at all.

function rudrAddToCart( product_id, quantity = 1 ) {

	// let's check is add-to-cart.min.js is enqueued and parameters are presented
	if ( 'undefined' === typeof wc_add_to_cart_params ) {
		return false;
		wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), 
			product_id: product_id, 
			quantity: quantity, // quantity is hardcoced her
		function( response ) {
			if ( ! response ) {
			// redirect is optional and it depends on what is set in WooCommerce configuration
			if ( response.error && response.product_url ) {
				window.location = response.product_url;
			if ( 'yes' === wc_add_to_cart_params.cart_redirect_after_add ) {
				window.location = wc_add_to_cart_params.cart_url;
			// refresh cart fragments etc
			jQuery( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash ] );

That’s it. Now you can use this function anywhere in JavaScript rudrAddToCart( 500, 1 );

If you’re interested in a kind of tutorial on how to get number of items in the cart, here it is.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X