On my own digital art store, I wanted to add a free set of postcards for every purchase over £20.

There’s different ways I could set this up, via a plugin or manually, using a coupon or simply run it automatically.
Plugins cost and of course, need maintaining etc, adding a coupon was OK, but then the customer has to do some manual work.
I wanted to make the journey simple and automatic.

I found the easiest way to achieve this was to create the FREE product and then use a function to automatically add said product to the cart.

Here’s how to achieve this using WooCommerce or ClassicCommerce:

PLEASE NOTE: ALWAYS BACK-UP EVERYTHING BEFORE TESTING AND USE A DEVELOPMENT SERVER, NEVER A LIVE SITE

First create your FREE product:
Create a FREE product - WooCommerce

Make sure you set ‘Catalog visibility: Hidden‘ otherwise the product will be listed in your shop as normal.
Also, check ‘Sold individually’ otherwise anyone can claim as many as they want!

Free product sold individually - WooCommerce

Once the product is set up, take a note of the product ID – this can be found in the edit page URL:
Product ID - WooCommerce

Next, add this code to your theme functions.php:

// Add free gifted product for specific cart subtotal
add_action( 'woocommerce_before_calculate_totals', 'check_free_gifted_product' );
function check_free_gifted_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// Settings
$free_product_id = 1189; // CHANGE THIS TO YOUR PRODUCT ID
$targeted_subtotal = 20; // CHANGE THIS TO THE REQUIRED LOWEST CART TOTAL

$cart_subtotal = 0; // Initializing

// Loop through cart items (first loop)
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// When free product is is cart
if ( $free_product_id == $cart_item['product_id'] ) {
$free_key = $cart_item_key;
$free_qty = $cart_item['quantity'];
$cart_item['data']->set_price(0); // Optionally set the price to zero
} else {
$cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
}
}

// If subtotal match and free product is not already in cart, add it
if ( ! isset($free_key) && $cart_subtotal >= $targeted_subtotal ) {
$cart->add_to_cart( $free_product_id );
}
// If subtotal doesn't match and free product is already in cart, remove it
elseif ( isset($free_key) && $cart_subtotal < $targeted_subtotal ) { $cart->remove_cart_item( $free_key );
}
// Keep free product quantity to 1.
elseif ( isset($free_qty) && $free_qty > 1 ) {
$cart->set_quantity( $free_key, 1 );
}
}

// Display free gifted product price to zero on minicart
add_filter( 'woocommerce_cart_item_price', 'change_minicart_free_gifted_item_price', 10, 3 );
function change_minicart_free_gifted_item_price( $price_html, $cart_item, $cart_item_key ) {
$free_product_id = 1189; // CHANGE THIS TO YOUR PRODUCT ID

if( $cart_item['product_id'] == $free_product_id ) {
return wc_price( 0 );
}
return $price_html;
}

Finally test the code by adding something to your cart ensuring the minimum cart total is reached and watch the FREE product get added automatically.

FREE product added to cart - WooCommerce

If you’re looking to set yourself up with an ecommerce store or just need help and advise on maximising your online presence get in touch today.