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

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 completely free. Needless to say, a comment and a social media share are much appreciated ๐Ÿ™‚

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

Requirements for “Free Sample” – WooCommerce

Before digging into the PHP, you will need to create a brand new simple product called exactly “Free Sample”.

Here are the requirements:

  • Title: “Free Sample”
  • Price: $0
  • Catalog Visibility: “Hidden”
  • Inventory: “Sold Individually”

Make sure to remember the product ID, which will be necessary in the PHP snippet ๐Ÿ™‚

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

 

/**
 * @snippet       "Free Sample" @ Single Product Page - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72796
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// -------------------------
// 1. Display Free Sample Add to Cart 
// Note: change "12345" 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="12345" 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', 10, 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: rename "Free Sample" to your free sample product name

add_filter( 'woocommerce_cart_item_name', 'bbloomer_alter_cart_item_name', 10, 3 );

function bbloomer_alter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
if ( $product_name == "Free Sample" ) {
$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', 10, 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 );
    }
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

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

  1. i have to show this button on specific products, how can i do that??

    1. Hello Sushma, I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  2. This is great. If I want to be able to disable it for certain categories how can I do this please.

  3. Hello, thank you for sharing this solution. Is there a possibility of assigning a fixed price to any sample order? Thanks!

    1. Hey Flavio, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  4. Hi Rodolfo,
    thank you very much for the snippet. It almost does as it is supposed to do. Unfortunately it doesn’t change the name of the Free Sample Product to the name of the actual product I put in my cart. Do you think it’s a mistake by my side or maybe the snippet needs to be updated as we are a couple of wordpress updates ahead?
    Thank you so much for the work you put into this!
    Kind regards,
    Christine

    1. Hello Christine, it works for me! You need to make sure your product is actually called “Free Sample” and that its ID is entered here:

      value="953"

      …otherwise it won’t work. Let me know

    2. Hi Rodolfo,
      okay, it works with that word. Unfortunately my customers speak German, so that is why I tried to translate Free Sample to Stoffmuster and just use the logic of the snippet. But there seems to be a restriction and the snippet just works with the English words.
      I would be happy to get a quote from you on custom work to customize the translation. See my request for a quote from Friday.
      Kind regards,
      Christine

    3. I answered to your email, thanks a lot!

  5. how can I pass the thumbnail image to the order? Do I need to utilise woocommerce_order_item_thumbnail ??

    thanks in advance

    1. Hello Sam, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  6. Hi Rodolfo. Thank you for making this functionality and making it available. I need to restrict it to a certain category, as not all products are suitable for free samples. However I cannot get it to work with the conditional logic that you refer us to try out. The functionality works perfectly without the conditional logic, but not with it (the button doesn’t show). If I just echo some text with the conditional logic script, that works fine. I hope you can help me in the right direction.

    1. Hello Simon, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  7. Dear Rodolfo.
    It is wonderful and still works with latest WooCommerce & WordPress.
    So easy to modify and just perfect! Thanks!

    1. Excellent ๐Ÿ™‚

  8. Wow! Great. It’s what I was looking for. Is there any way to show Free Sample button for specific categories only? I have a store. I like to offer free samples for perfumes not for make-up.

    1. Hello Kibria – thanks so much for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  9. Hi Rodolfo,

    What a great snippet of code! Just what I was looking for.

    in Step 4. (to my very limited PHP understanding) it looks like $product_name should add the product name to “Free Sample”.
    ie: Free Sample (Product Name).

    Is this correct? if so, it doesn’t seem to work for me. It still just says “Free Sample”.

    Looking forward to your response ๐Ÿ™‚

    1. Thank you Werner! No, at step 4 I concatenate (Free Sample) to the existing Product Name in the Cart and Checkout. It works in my development site ๐Ÿ™‚

    2. Hmm, it doesn’t seem to work for mine running the latest version of WC and WP. Aparently woocommerce_add_order_item_meta is depreciated in the latest version of woocommerce? Could this be why?

    3. Possible ๐Ÿ™‚ But probably you’re using an old version of this snippet, I already use “wc_add_order_item_meta” in here. Try revising your snippet and let me know

  10. hey hey this is wicked. and just what I needed so thanks very much

    Thanks for this. is there a way to log the time difference between the customer ordering the sample then coming back to order the full product?

    Thanks again

    Nick

    1. Hey Nick, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  11. p-Content/Plugins/Code-Snippets/Php/Snippet-Ops.Php(352) : Eval()’D Code On Line 58

    i get a php error in the cart when i used this

    1. Hey David! What’s on line 58?

    2. just a curly brace. strange thing is ive used this exact snippet before without issue tested on another install and it worked fine maybe a theme issue?

  12. Hello again, does anyone know how “not” to show this ‘Order Sample’ for certain products that don’t have samples available? In other words, is there a quick easy way to turn this sample feature off for specific products? Thanks in advance.

    1. Hey Rod, thank you so much for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  13. Dam good! this was a super usefull snippet:) ! Question. Is there a way of also putting the product name you are ordering in the cart?

    Free sample : Product name

    1. John, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  14. Hi can this be changed to be able to select the categories it needs to apply to?
    Also I would need it implementing to the woomobify plugin too if this possible?
    Thanks and look forward to your reply

    1. Hi Colin, thanks for your comment! Yes, I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  15. Hi there! This worked great!… I was able to also add the button to the main products display page (i.e. shop page). Thank you.

    1. Brilliant, thanks Rod ๐Ÿ™‚

  16. Do you offer the customization services necessary for this code’s additional functionalities mentioned by others here? I also need the ability to “Order Free Samples” with some additional function/conditions. Please let me know, thank you.

    1. Hello Rod, thanks for your comment (and great name by the way :)). Yes, I’m available every day for paid work – if you’d like to get a quote, feel free to contact me here. Thanks in advance! ~R

  17. Great piece of code, but I can’t seem to add a shipping option to the free sample – any idea how I would do this please? My product sample is free, but the customer will need to pay postage, so I need to allocate a shipping class to it. I’ve added this normally in Product Data – Simple Product > Shipping but it doesn’t seem to pick it up correctly. All help gratefully received!

    1. Ian, thanks so much for your comment! Unfortunately this is custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

  18. This is amazing, the 2 plugins I found were so hit and miss. There is one thing extra I need it to do, is there any way to make it copy the SKU as well as the product names. I am using it on a carpet store and different carpets are annoyingly called the same thing.

    1. Andy, thanks so much for your comment! Yes, this is definitely possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  19. Can the button be unique to categories as appose to all products? this would be greatly helpful for me.

    1. Chris, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

    2. Hi Chris. Did you ever find a solution for this – I have the same issue as you?

    3. In case anybody is still looking for a way to make the button appear only for certain categories, the solution provided here worked for me: https://stackoverflow.com/questions/45118802/display-a-custom-button-on-woocommerce-product-pages-only-for-certain-categories

      Basically modified Rodolfo first bit of code to this:

      
      // -------------------------
      // 1. Display Free Sample Add to Cart 
      // Note: change "12345" with Free Sample ID
       
      add_action( 'woocommerce_single_product_summary', 'bbloomer_add_free_sample_add_cart', 35 );
       
      function bbloomer_add_free_sample_add_cart() {
      global $product;
      $product_cats = array('category1','category2','category3');
      if( has_term( $product_cats, 'product_cat', $product-&gt;get_id() ) ){
      ?&gt;
      
      Order a Free Sample
      &lt;input type=&quot;hidden&quot; name=&quot;free_sample&quot; value=&quot;"&gt;
       
      &lt;?php
      }
      }
      
      

      Hope this helps ๐Ÿ™‚

  20. Hi Rodolfo,
    Can Woo ensure only one free sample offer is made available to just one unique customer, to ensure unlimited free samples are not being ordered by the same person per order or per day or week etc?

    1. Denis, thanks for your comment! Yes, an additional snippet can be added to limit the Cart to only 1 free sample, similar to this (additional code is required): https://businessbloomer.com/woocommerce-allow-1-product-cart/

  21. You are a genius. Thank you so much for this.

    1. Thanks for your feedback Olga ๐Ÿ™‚

  22. Excellent, this helped a lot. Business Bloomer Forever!

  23. Now we’re talking! this is a hella dope snippet.

    More from that please!

    1. Great to hear that Ahmed ๐Ÿ™‚

  24. You are awesome!

    1. Ahhhh, thank you Faizan ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.