Created
January 28, 2014 10:41
-
-
Save kloon/8665458 to your computer and use it in GitHub Desktop.
WooCommerce add product to cart on visit
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
<?php | |
// Place the following code in your theme's functions.php file | |
add_action( 'init', 'wc_add_product_to_cart_on_visit' ); | |
function wc_add_product_to_cart_on_visit() { | |
if ( ! is_admin() ) { | |
global $woocommerce; | |
$product_id = 64; // The ID of the product to add to cart | |
$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 ); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment