Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Free Sample Product With Every Purchase in WooCommerce?

Are you interested in leveraging BOGO discounts to attract more customers to your WooCommerce store with complimentary product samples? Your search ends here! With the following snippet, you can effortlessly include a free sample product in your customers’ carts. Let’s discover how to seamlessly ‘Add free sample product in WooCommerce’!

Solution: Add a Free Sample Product With Every Purchase in WooCommerce

The below code snippet adds a button labeled “Order a Free Sample Product” to the product pages. This button allows customers to easily add a specific free product to their cart by clicking it.

add_action( 'woocommerce_single_product_summary', 'ts_add_free_sample_add_cart', 35 );
  
function ts_add_free_sample_add_cart() {
   ?>
      <form class="cart" method="post" enctype='multipart/form-data'>
      <button type="submit" name="add-to-cart" value="34" class="single_add_to_cart_button button alt" style="background-color: #FFBF43">Order a Free Product</button>
      <input type="hidden" name="free_sample" value="<?php the_ID(); ?>">
      </form>
   <?php
}
  
add_filter( 'woocommerce_add_cart_item_data', 'ts_store_free_sample_id', 9999, 2 );
  
function ts_store_free_sample_id( $cart_item, $product_id ) {
   if ( isset( $_POST['free_sample'] ) ) {
         $cart_item['free_sample'] = $_POST['free_sample'];
   }
   return $cart_item; 
}
  
add_filter( 'woocommerce_cart_item_name', 'ts_alter_cart_item_name', 9999, 3 );
  
function ts_alter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
   if ( 34 === $cart_item['product_id'] ) {
      $product = wc_get_product( $cart_item["free_sample"] );
   }
   return $product_name;
}

Output

When a customer adds the product i.e. “Hoodie with Logo” to their cart on the single product page, they can also add a free sample product by clicking the button below.

Add a Free Sample Product

Upon redirecting to the cart page, the customer will see two products: a Hoodie with a Logo and a free sample product – a Tshirt with Logo.

How to Add a Free Sample Product With Every Purchase in WooCommerce? - Tyche Softwares

It also displays two products on the checkout page, i.e. Hoodie with Logo and a free sample product (Tshirt with Logo).

How to Add a Free Sample Product With Every Purchase in WooCommerce? - Tyche Softwares


I hope this guide helped you set up a basic BOGO offer. If you’re looking to add products to your WooCommerce cart automatically based on conditions like cart total, category, or website visits, explore more advanced strategies that we have covered.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x