Adding Free Sample or Tester Products in Your Magento Shop

Today here at Web Design GM are going to discuss how to add sample or tester products to your Magento shops. There are many old tutorials which are out of date and simply do not work. We have tested this solution with Magento version 1.5 and 1.7. Free sample products or free tester products are ideal for e-commerce websites that require giving their customers for example tester materials or sample perfumes for instance.

You can restrict the amount of free sample products you want give out to your clients because you do not want people to have let’s just say a 100 free sample products. instead this tutorial will restrict the sample product to 5 but you can edit the code you change it to whatever you feel is the right amount.

1. Firstly you will need to create a file called getSample.php in the root of your Magento installation folder and populate it with the following code.


ini_set('display_errors', 1);


require_once 'app/Mage.php';

Mage::getSingleton("core/session", array("name" => "frontend"));
// Store users session
$session = Mage::getSingleton("customer/session");
//Store cart items
$totalNumOfCartItem = Mage::getModel('checkout/cart')->getItemsQty();

//Store cart items
$theitems = Mage::getModel('checkout/cart')->getItems();

$thecount = 0;

foreach($theitems as $ti){

	$thename = $ti['name'];

	$test = strpos($thename,'Free Sample');

	if($test !== false){
	$thecount = $thecount+1;


$name = $_POST['name'];

$id = $_POST['id'];
if(isset($_POST['simpleid']) && !empty($_POST['simpleid'])){
$simpleid = $_POST['simpleid'];
$name = $_GET['name'];
if(isset($_GET['simpleid']) && !empty($_GET['simpleid'])){
$simpleid = $_GET['simpleid'];
$id = $_GET['id'];

// retrict shopping cart to only allow 5 samples 
if ($totalNumOfCartItem <= 4)
$theproduct = Mage::getModel('catalog/product')->load($id);

$thesimple = Mage::getModel('catalog/product')->load($simpleid);

if($thecount < 2){

// instatiate Product
$product = Mage::getModel('catalog/product'); 
} else {
$product->setName("Sample of ".$name);
// $product->setDescription("Sample of ".$name);
$product->setShortDescription($name." - Sample");

$product->setImageUrl($theproduct->getImageUrl()); // set the images from the original product
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery($_SERVER['DOCUMENT_ROOT']."/media/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false); 
$product->setAttributeSetId(9); // need to look this up
$product->setCategoryIds("15"); // need to look these up
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(1); // catalog, search
$product->setStatus(1); // enabled 
// assign product to the default website

// get product's general info such price, status, description

$stockData = $product->getStockData();

// update stock data using new data
$stockData['qty'] = 1;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['max_sale_qty'] = 5;

// then set product's stock data to update

// call save() method to save your product with updated data

header("Location: /checkout/cart/add/product/".$product->getId()."/"); 

Mage::getSingleton('checkout/session')->addError("You are only allowed a maximum of 5 sample products");

header("Location: /checkout/cart");

The above code is simply the function which will create sample products for you on the fly. Also not that in this example we have assigned each sample product with a ridiculous weight because the client offered the sample product and the shipping of the sample products for free meaning we had to create a table rate for the shipping method which did not interfere with the other shipping logic so that anything that exceeds 21212121 will have free shipping.

2. Next you will need to create a button where you want people to add a free sample product in the shopping basket. I have placed my sample button in the list.phtml and placed the button in both my list view and grid view.

<form action="/getSample.php" method="post">
      <input type="hidden" name="id" value="<?php echo $_product->getId() ?>" />
      <input type="hidden" name="name" value="<?php echo $this->htmlEscape($_product->getName()) ?>" />
      <input type="image" name="getsample" alt="Get Sample for <?php echo $this->htmlEscape($_product->getName()) ?>" src="<?php echo $this->getSkinUrl('images/your_theme_name/sample-btn.jpg') ?>" />

The above will execute the getSample.php and add a sample product to your shopping basket whilst still allowing you to purchase other products from the Magento shop.

Related posts:

  1. How to Import Products in Magento Tutorial
  2. Magento: Adding an RSS Feed
  3. Tutorial 2 – Adding A Language Pack To Magento
  4. Google Products For Web Developers
  5. Magento: Changing The Favicon

Leave a Reply

Contact Us Today...

If you have any questions about our services or want to arrange a free no obligation consultation contact us today or call 02920 290 080 for Cardiff and 01179 000 482 for Bristol.

Bit Torrent Study Discovers Most File Sharers Are Now Monitored

Ever heard of torrents or used Bit Torrent to download movies, music or shared files over the internet? Researchers have discovered that anyone who does use Bit Torrent to download files over the World Wide Web will most probably be monitored. Studies were carried out by Birmingham University and they have discovered that if someone [...]
» more

Cardiff: 02920 290 080 Bristol: 01179 000 482