Menu Close

WooCommerce: Order a “Free Sample” @ Single Product Page

default

Recently I was on a coaching call with a client and the “Free Sample” challenge came up. Client has 400+ products on the website and had no intention of adding a free variation to each product manually.

So, I promised to myself I was going to study a different approach. And today you get it for free – nice! Needless to say, a comment and a social media share are much appreciated. Enjoy!

Display an “Add Free Sample to Cart” button @ WooCommerce single product page

Requirements for “Free Sample” – WooCommerce

Before digging into PHP, you will need to create a brand new simple product (otherwise the code won’t work).

Here are the requirements:

  • Title: “Free Sample” (or call it whatever you like, but make sure to change the PHP snippet below accordingly)
  • Price: “0
  • Catalog Visibility: “Hidden
  • Inventory: “Sold Individually

Make sure to remember the product ID of this new product, because you will need to use it in the PHP snippet.

PHP Snippet: “Free Sample” Add to Cart Button @ WooCommerce Single Product Page

/**
 * @snippet       Add Free Sample to Cart @ Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// -------------------------
// 1. Display Free Sample Add to Cart 
// Note: change "123" with Free Sample ID
 
add_action( 'woocommerce_single_product_summary', 'bbloomer_add_free_sample_add_cart', 35 );
 
function bbloomer_add_free_sample_add_cart() {
	?>
		<form class="cart" method="post" enctype='multipart/form-data'>
		<button type="submit" name="add-to-cart" value="123" class="single_add_to_cart_button button alt">Order a Free Sample</button>
		<input type="hidden" name="free_sample" value="<?php the_ID(); ?>">
		</form>
	<?php
}
 
// -------------------------
// 2. Add the custom field to $cart_item
 
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_store_free_sample_id', 9999, 2 );
 
function bbloomer_store_free_sample_id( $cart_item, $product_id ) {
	if ( isset( $_POST['free_sample'] ) ) {
			$cart_item['free_sample'] = $_POST['free_sample'];
	}
	return $cart_item; 
}
 
// -------------------------
// 3. Concatenate "Free Sample" with product name (CART & CHECKOUT)
// Note: change "123" with Free Sample ID
 
add_filter( 'woocommerce_cart_item_name', 'bbloomer_alter_cart_item_name', 9999, 3 );
 
function bbloomer_alter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
	if ( 123 === $cart_item['product_id'] ) {
		$product = wc_get_product( $cart_item["free_sample"] );
		$product_name .=  " (" . $product->get_name() . ")";
	}
	return $product_name;
}
 
// -------------------------
// 4. Add "Free Sample" product name to order meta
// Note: this will show on thank you page, emails and orders
 
add_action( 'woocommerce_add_order_item_meta', 'bbloomer_save_posted_field_into_order', 9999, 2 );
 
function bbloomer_save_posted_field_into_order( $itemID, $values ) {
    if ( ! empty( $values['free_sample'] ) ) {
		$product = wc_get_product( $values['free_sample'] );
		$product_name = $product->get_name();
		wc_add_order_item_meta( $itemID, 'Free sample for', $product_name );
    }
}

Is there a Reliable Plugin for that?

If you’d love to code but don’t feel 100% confident with PHP, I decided to look for a reliable plugin that achieves the same result.

In this case, I recommend the WooCommerce Product Sample plugin. On top of letting your customers purchase free and paid samples of your products, the plugin also helps you send follow-up email reminders for sample orders and much more.

But in case you hate plugins and wish to code (or wish to try that), then keep reading!

View Source
Posted in WooCommerce Tips