Created
December 7, 2013 11:18
-
-
Save corsonr/7839908 to your computer and use it in GitHub Desktop.
Automatically add product to cart on visit - WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Add item to cart on visit | |
*/ | |
function add_product_to_cart() { | |
if ( ! is_admin() ) { | |
global $woocommerce; | |
$product_id = 64; | |
$found = false; | |
//check if product already in cart | |
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { | |
foreach ( $woocommerce->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 ) | |
$woocommerce->cart->add_to_cart( $product_id ); | |
} else { | |
// if no products in cart, add it | |
$woocommerce->cart->add_to_cart( $product_id ); | |
} | |
} | |
} | |
add_action( 'init', 'add_product_to_cart' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi corsonr,
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