WooCommerce: Order a Free Sample of product on the product page

Need to offer a sample version of the product your selling? Then this guide will help you offer your woocommere store just that. It adds a button on the product page near the ‘Add to Cart’ button and will add the sample to the cart.

How it works

You will create a product called ‘Sample Product’ and offer it on every product in your store.

// 1. Display Free Sample Add to Cart 
// Note: change "4926" 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' action="">
		<button type="submit" name="add-to-cart" value="4926" class="button">Order 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. Preserve the custom field in the session
 
add_filter( 'woocommerce_get_cart_item_from_session', 'bbloomer_get_cart_items_from_session', 10, 2 );

function bbloomer_get_cart_items_from_session( $cart_item, $values ) {
	if ( isset( $values['free_sample'] ) ){
		$cart_item['free_sample'] = $values['free_sample'];
	}
	return $cart_item;
}
 
// -------------------------
// 4. Concatenate "Free Sample" with product name (CART & CHECKOUT)
 
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;
}
 
// -------------------------
// 5. 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 );
    }
}

// -----------------------
// 6. Display Meta Data Sample on Cart

function iconic_display_engraving_text_cart( $item_data, $cart_item ) {

    if ( empty( $cart_item['free_sample'] ) ) {
        return $item_data;
    }

    $product = wc_get_product( wc_clean($cart_item['free_sample']) );

	$sample_title = $product->get_title();
 
    $item_data[] = array(
        'key'     => __( 'Sample for ' ),
        'value'   => $sample_title,
    );
 
    return $item_data;
}
 
add_filter( 'woocommerce_get_item_data', 'iconic_display_engraving_text_cart', 10, 2 );

Leave a Reply

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