fbpx
Woocommerce add to cart function programmatically

WooCommerce add to cart function programmatically

Are you looking for a WooCommerce add to cart function that allows you to improve customer experience and increase your sales? Today’s your lucky day because you’ve just found it! We’ve already seen some of the best themes and top plugins for your eCommerce website but if you don’t feel like downloading or buying anything, there are some other things you can do on your own to improve your store.

Add to cart function programmatically in WooCommerce

In this post, you’ll learn how to apply an “add to cart” function programmatically using a WordPress hook so users can find the products they want in the checkout easily. This can be useful in several situations:

  • When you’re giving away products
  • Offering a free product when customers purchase something
  • In one-product-stores, some customers won’t even need to add anything to the cart. As the product will be added to the cart when the users go to the checkout, it’ll be already there.

So now that we know when to use it, let’s have a look at the script of this WooCommerce add to cart function:

add_action( 'template_redirect', 'quadlayers_add_to_cart_function' );

function quadlayers_add_to_cart_function () {

    $product_id = 1326;

    if ( WC()->cart->get_cart_contents_count() == 0 ) {

        WC()->cart->add_to_cart( $product_id );

    }

}

As you can see, it’s a very simple script. Let’s break it down and analyze each part:

The hook

add_action( 'template_redirect', 'quadlayers_add_to_cart_function' );

Here we are using the template_redirect hook provided by WordPress. This action hook runs before the template of a page is loaded. As the checkout page has a template, this hook allows us to add some logic before it loads. If you’re not familiar with hooks, this guide is a great starting point. And to dig deeper into the checkout hooks, we recommend you have a look at this tutorial with actionable examples.

The WooCommerce add to cart function

$product_id = 1326;

Indeed, the $product_id variable isn’t fully necessary as you could enter the id directly like this:

WC()->cart->add_to_cart( 1326 );

However, if you want to add a more complex logic, you’ll need to use variables. Additionally, with the $product_id variable, the code is easier to understand and helps keep it organized, something very important when dealing with long lines of code.

if ( WC()->cart->get_cart_contents_count() == 0 )

Afterward, we check if the cart is empty using an if() conditional. This way, we make sure that we don’t change the user cart content if he has already included a product.

WC()->cart->add_to_cart( $product_id );

After that, we add the WC()cart->add_to_cart() function in the conditional. This is what gets the job done. The native WooCommerce add to cart function is always preceded by WC(), so to have clean and organized code, you should write it after WC() as follows:

WC()->add_to_cart

Finally, we only need to specify the product id and voilá! Your WooCommerce add-to-cart function will be ready to go!

How to add more products to the add-to-cart function

If you have an online store, you’re likely to sell several products so you’ll be able to add them to the add-to-cart function. Easy peasy. You just need to copy and paste the same WooCommerce add-to-cart function changing the product ID. For example:

WC()->cart->add_to_cart( 1 );
WC()->cart->add_to_cart( 3 );
WC()->cart->add_to_cart( 2 );
WC()->cart->add_to_cart( 6 );

Another option is by using variables:

$product_id_1 = 13;
$product_id_2 = 26;
WC()->cart->add_to_cart( $product_id_1);
WC()->cart->add_to_cart( $product_id_2);

How to add product quantity?

Now, what if you need to add the same product several times? Just modify the WC() function as follows:

WC()->cart->add_to_cart( $product_id_1, 5 );

In the above example, you’ll be adding the product with id_1 five times.

How to add a product only once

Another great trick that might be very useful if you want to include the product in the cart only once is to use the “find_product_in_cart()” function before the execution. With this function we can check if the product is already in the cart using an if() conditional. This way, we make sure that we don’t add the product twice if the user leaves the checkout page and comes back later.

add_action( 'template_redirect', 'quadlayers_add_to_cart_programmatically' );
function quadlayers_add_to_cart_programmatically() {
   $product_id = 1326;
   $product_cart_id = WC()->cart->generate_cart_id( $product_id );
   if(!WC()->cart->find_product_in_cart( $product_cart_id )) {      
       WC()->cart->add_to_cart( $product_id);
   }
}

How to add a notice after addition

Finally, let’s have a look at how to add a notice after an addition:

add_action( 'template_redirect', 'quadlayers_add_to_cart_programmatically' );
   
function quadlayers_add_to_cart_programmatically() {
   $product_id = 1326;
   $product_cart_id = WC()->cart->generate_cart_id( $product_id );
   if(!WC()->cart->find_product_in_cart( $product_cart_id )) {
       WC()->cart->add_to_cart( $product_id);
       wc_print_notice( 'Product ID ' . $product_id . ' is in the Cart!', 'notice' );
   }
 
}

That’s it, guys! By now you should have a WooCommerce add-to-cart function up and running on your store. Let us know if you have any questions and share your experience with us in the comment section below.

Finally, don’t forget to check out our guides:

Hello!

Click one of our representatives below to chat on Telegram or send us an email to [email protected]

How can I help you?