Answer the question
In order to leave comments, you need to log in
WooCommerce! Auto add 4 items to cart how?
Help with WooCommerce! Automatic addition of 4 items to the cart. Experience in development 0. I'm doing it for the first time, so it's a bit tight. I found this code in the open spaces, which completely suits me, but I can’t make it so that 4 DIFFERENT products with DIFFERENT IDs are added at the same time. Help me please!
/**
* Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 772; //replace with your product id
$found = false;
$cart_total = 1; //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 );
}
}
}
}
Answer the question
In order to leave comments, you need to log in
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$articles = array(157,158,159,160);
$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 (($key = array_search($_product->id, $articles)) !== false)
unset($articles[$key]);
}
// if product not found, add it
if ( count($articles) > 0 ) {
foreach ($articles as $article) {
WC()->cart->add_to_cart($article);
}
}
} else {
// if no products in cart, add it
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question