woocommerce automatically add products to cart on visit,automatically add product to cart woocommerce,auto add product to cart woocommerce,how to add product to cart in woocommerce programmatically,add product in cart woocommerce,add to cart option in woocommerce,add multiple products to cart in woocommerce,how to add product to cart using ajax in woocommerce,add to cart in woocommerce

woocommerce automatically add products to cart on visit

Automatically add products to cart on visit

Below Code example, automatic add products to cart when a visitor is on your site. This code example could be very useful when you use WooCommerce. This is is pretty easy to do. You only need is a little snippet of code to add to functions.php, in your theme folder. You can also add multiple products like this.

To add a product into the cart, simply find its ID, and then replace the default value in the code snippet below. You have to avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme. To solve this problem you should have to create a child theme.

/*
* Add Woocommerce product to cart on customer visit
*/
function add_your_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 50;
$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' );

If you want to automatically add a product to the cart based on the cart total use the following code:


add_action( 'init', 'add__your_product_to_cart' );
function add__your_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 68; //replace with your product id
$found = false;
$cart_total = 30; //replace with your cart total needed to add above item

if( $woocommerce->cart->total = $cart_total ) {
//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->get_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 );
}
}
}
}

woocommerce automatically add products to cart on visit,automatically add product to cart woocommerce,auto add product to cart woocommerce,how to add product to cart in woocommerce programmatically,add product in cart woocommerce,add to cart option in woocommerce,add multiple products to cart in woocommerce,how to add product to cart using ajax in woocommerce,add to cart in woocommerce

Post Created 130

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top