Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active March 5, 2020 08:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kloon/2376300 to your computer and use it in GitHub Desktop.
Save kloon/2376300 to your computer and use it in GitHub Desktop.
WooCommerce Automatically add product to cart on site visit
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
@itskylem
Copy link

Love this! Is there any way to prevent the item from being re-added if the user tries to delete it? Like a stop command?

@kloon
Copy link
Author

kloon commented Dec 5, 2012

Only seeing the comment now, it has been altered to check if the product is already in the cart.

@emminnn
Copy link

emminnn commented Mar 11, 2014

Hi,

Could you please write how we can add to the cart based on a certain user role i.e. "customer"? So instead of -if not admin-, I only need to enter: -if customer- . It'd be so appreciated!

Thanks!
Emin

@tsfact
Copy link

tsfact commented Dec 29, 2015

Hi kloon,
First of all, thank you for your time making this snippet.
I have tried this one but it seems that the "user" cannot delete the added item if wish, which mean that this snippet is useless & counterproductive. If the users cannot delete the item (apart from annoying them a lot) then they will never go to checkout with a item that they do not want to. In other words, the potential client would be lost.

If there is a way to fix this, I would appreciate a reply.

Many thanks

@iaremarkus
Copy link

Same comment as the above guys...

But if they choose to set the quantity to zero in order to remove the product, I don't want to keep adding it back for them.

Perhaps set some session/cookie info to confirm that the item was removed. In this way, the code that adds the product, can be wrapped in a conditional to only execute if the user hasn't already said "no i dont want this".

@sc0ttkclark
Copy link

This doesn't quite do it for me, was looking at having this integrate directly with WC One Page Checkout, see my forked code which does a few other things as well as simplifies the primary logic too.

https://gist.github.com/sc0ttkclark/839dbad77c62fba36d1368718bb4e982

@shreyasborse
Copy link

Thanks @sc0ttkclark I don't understand why WooCommerce official documentation is still showing outdated code.

It wasted my time, thank you for posting the link.

@tsunamimarketing2
Copy link

Is there a way to have it only add the product to the cart if a specific coupon code is applied?

@urbanadventurer
Copy link

WooCommerce raises a warning about accessing ID directly. It needs this change.

if ( $_product->get_id() == $product_id )

@mariuszo73
Copy link

Is it possible to have simliar funcionality but for condition: “Add product to cart when user choose speicified shipping”

I would like to add a cardboard packaging for every order shipped by post.
It is neccessary for proper calculation of total order weight and shipping cost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment