Quite an interesting functionality! A WooCommerce client wanted their Cart pre-filled with one product as soon as their customers accessed the website. I don’t remember the exact reason, but this could be useful when you want to give them a free product by default, or you want to send your visitors straight to checkout with a product already in the cart without letting them add anything first.
Adding an item to cart programmatically is the same as “automatically”. Basically, all users will have a default, non-empty Cart filled with an item of your choice. So, let’s see how this snippet works!
PHP Snippet: Add Product to WooCommerce Cart Automatically On Visit
/**
* @snippet Add Product to WooCommerce Cart Programmatically
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 3.9
* @community https://businessbloomer.com/club/
*/
add_action( 'template_redirect', 'bbloomer_add_product_to_cart_automatically' );
function bbloomer_add_product_to_cart_automatically() {
// select product ID
$product_id = 21874;
// if cart empty, add it to cart
if ( WC()->cart->get_cart_contents_count() == 0 ) {
WC()->cart->add_to_cart( $product_id );
}
}
When using the Variable product doesn’t work.
Hi Abdelrahman, did you try use the variation ID?
For some reason this function breaks the Rest API of my site. Added the following on top to prevent that from happening.
[php]
if(!WC()->cart ){
return;
}
[php]
Thanks!
Super helpful – thank you!
Yay!
Hello, I have read all your posts and I really appreciate your work, it has helped me enormously.
I wanted to ask you a question to see if you could help me, I have already managed to add products programmatically but now I need to add a product to the cart but that it is hidden from the buyer but that it appears in the order. This to be able to identify the vendor of the product through a hidden product without complications other than adding a product for each vendor you have. Without installing another plugin or more complications with commissions.
I would appreciate your help and I look forward to your response
Hello Jesus! Did you try setting up the product as “hidden”?
Hi Rodolfo, I’ve been working off a few of your guides (which are great) but I’m stuck on one issue. I’m using WooCommerce strictly as a catalog site directing users to reach out for actual purchases, but have integrated the shipping calculator on the individual product pages in order to provide shipping estimates with ajax. The problem is that when the request goes through, WooCommerce registers that the cart is empty, and the shipping classes I’ve set up don’t engage so the price only comes out as the flat rate. So essentially what I want to do is automatically add 1 of the product to the cart each time an individual product page loads (broken out by category). And then have the script empty the cart and add 1 on any subsequent visit to a different individual product page. I tried based on the global $products id but am having no luck. Any notes would help. Thanks!
Ciao Angelo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Man! I am so happy with this code. I have used it for a couple of weeks and also the customers are happy with my ‘suggestion’.
Saying that, I will add some free products to my shop and when these (category) are in the cart, then this extra product must NOT be added.
So if found some code what to do when the cart has a certain cat, but then I get lost with this action.
Can you help me out on that?
Thanks so much for your comment! If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
thanks for the code. but there is a way to prevent customers to delete this product ??
Hi Fadi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Thanks so much, Mr. Rodolfo for giving us this. I used this today the 15th of April for a client’s work and it works perfectly
Cool!
When using
, how can I refresh the cart so the changes are reflected?
Uhm, maybe you need to “AJAXify” the snippet. I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi, Can you Add Product to Cart Programmatically depending on user role?
Thanks, Cam
Hi Cam, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello
I want to have a product link that one product add to cart automatically and the user goes to the checkout page directly.
Please help me, how can I do this?
thanks
Saeed, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Salve,
vorrei che la quantitΓ del prodotto inserito partisse da zero e non da uno Γ¨ possibile?
oppure
Aggiungere questo prodotto sono nel caso in cui il cliente scelga una determinata variazione degli attributi dei prodotti presenti nello shop
Ciao Denise, you can’t add an item to cart with quantity = 0 π
Hi Rodolpho,
Great work as usual.
I want to use your code but to show a different product depending on the amount of items.
I was thinking of using array but I can’t seems to get it right.
I would really appreciate if you or anybody could give me the right code.
That’s what I have so far:
No I wouldn’t use an array. Just put the product ID inside the add_to_cart(XYZ) function
Hi, lately Iβm working on. Few products and Iβm googling quite a lot with a lot of questions here and there and your site pops up most of the time with really clever approaches to the solutions. So for that part, thanks a lot.
I was wondering if it is possible so I can add more than one of the same product Iβd (so qty increase) and out of curiosity and maybe others, add more than one different Id
Thanks kind regards
Hey Andres, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hello,
What do you replace “bbloomer_add_product_to_cart” with? Am I suppose to change the “bbloomer” part?
Hey there, thanks for your comment! Nothing if you like. That’s just the name of the function I came up with. You can use “my_custom_function_name” if you prefer π
Hi Rodolfo, i was trying to do something similar to this but it has to be done client side, is there a an add to cart function that uses javascript instead of php?
I’m trying to add a variable product where the variable is decided after some input from the user (not them deciding which one like the base dropdown does).
I’ve been struggling with ways to do this for a few weeks and any help you could provide would be most appreciated.
Hey Tyler, thanks for your comment! Maybe this might help: https://wordpress.stackexchange.com/questions/53280/woocommerce-add-a-product-to-cart-programmatically-via-js-or-php
Hi Rodolfo
this is very helpfull
i hope i can read more resources from you again in the future
keep it up!
Thank you π
I have found many code for this on internet but all are producing an error. At last I have found this and this snippet is working fine without any error.
Thanks Rodolfo.
Excellent π
Is there a way to add a custom meta for each individual cart item?
“custom_reference” and “price” comes from an add-on textbox.
I was able to modify the price for each individual cart items, but I can’t seem to make the custom_reference work.
cart item 1: https://yourdomain.com/?add-to-cart=25&price=100&custom_reference=REF001
cart item 2: https://yourdomain.com/?add-to-cart=25&price=990&custom_reference=REF002
cart item 3: https://yourdomain.com/?add-to-cart=25&price=500&custom_reference=REF003
custom_reference & price comes from a variable that is being passed to the query string
https://yourdomain.com/?add-to-cart=25&price=var_1&custom_reference=var_2
I think this will help the others as well π
This is something very useful especially for products with extended variable pricing and referencing.
Hey Eejay, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
hi is it possible to modify the price of this product_id?
The API is
add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
Any tips on which params should i modify to change the price?
Thanks!
Hey Irvin, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hi Rodolfo,
This is really helpful, thank you! I’ve had issues with a payment plugin, when the order is created by the admin and then customer goes to pay via payment link or from the account, the payment is denied (if) because the cart is empty.
As I’ve searched for solution, I happened to google a lot of similar problems unsolved.
But I’ve noticed that if there is anything in the cart, the payment goes through successfully (and it does not matter what, because the order totals does not depend on the cart in this case). So adding a single product in the beginning of the payment plugin function
worked just fine.
Yet, it is not a universal solution (since you need to add certain product id), can you suggest anything better to bypass that “requirement” for a cart not to be empty if it is a payment-link order?
Hey Slon, thanks for your comment! The cart shouldn’t be empty, try some troubleshooting: https://businessbloomer.com/woocommerce-solving-cart-empty-issue/
Hi Rodolfo!
Is it possible to programmatically modify the price of a product only for a particular customer if it enters a promotional code and add it to the cart?
Thank you!
Hey Irwin, thanks for your comment! You can just create a product-specific coupon and assign it to a single user email if you wish π
Hi Rodolfo, quick question – is it possible to allow the customer to remove the product from the cart if they so choose as opposed to it simply staying there regardless?
Hey Bri, thanks for your comment! I’m not sure I fully understand your question – can’t a user go to the Cart and remove the item if she wishes?