U
U
user20012020-08-21 12:54:23
WooCommerce
user2001, 2020-08-21 12:54:23

WooCommerce! Automatically add items to the cart for every thousand in the cart, how to do?

WooCommerce! Automatically add items to the cart for every thousand in the cart, how to do?
there is this code, it adds 3 different products to the cart when a person enters the site, but I want one more product to be added if the person has an order for 1 thousand, 2 identical products if there are 2 thousand, etc.
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$articles = array(157,158,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 );
}
}
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question