WooCommerce Automatically Adding the Product to the Cart

function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 30;
if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin() ) {
$free_product_id = 1991; // 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 );
wc_add_notice( sprintf( '30TL üstü siparişiniz için Mor Tortilla ücretsiz eklendi.', '' ) );
} 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' );

Written by Uğur

Leave a Reply

Your email address will not be published. Required fields are marked *