using the code found on Internet, works perfectly, but it needs to be updated with extra function.
now: if total sum gets 100+ free gift is added. if user removes some products and total gets lower than 100, free gift still stays there
should be: if user removes products and total gets lower than 100, free gift is removed
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 100;
if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin() ) {
$free_product_id = 6663; // Product Id of the free product which will get added to cart
$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 ( $_product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
}
add_action( 'template_redirect', 'aapc_add_product_to_cart' );
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…