1. Home
  2. Prices
  3. Guided Tour
  4. New Shop
  5. Shop Review
  6. Ecommerce Marketing
  7. Ecommerce TV
  8. Magento Developers
  9. Portfolio
  10. Blog
  11. Contact
Home > Magento Tutorials > Magento Free Samples Solution

Magento Free Samples Solution

Posted on: 30th Mar 2010 By: Robert Kent Leave a comment 6 Comments

A few months ago I was tasked with creating a free samples feature for magento – of every product. Now this is a little tricky as all of the cart functionality needed to stay alive for the actual purchase of the products.

I came up with something of a little hack that suited both myself and the client and also the customer.

Basically the way I went around creating a free sample feature was to charge the customer – weird huh? The problem was the client wanted to charge for postage and packaging anyway so the addition of 1p per product didn’t seem all that bad.

The simple fact of the matter is that it worked. I received a few email notifications of sample purchases that have turned into full purchases. For example one customer bought 5 lots of samples of the same type of tile. 3 days later after delivery of the sample 10cmx10cm tiles the same customer went back on to the website and bought £650 worth of two of those samples.

For me, this justifies the slight headache that was the free samples solution. The solution that I am going to show you now…

1. Create a file called getSample.php and place it in your home directory.

2. Paste in the following code:

require_once 'app/Mage.php';
Mage::app();

$name = $_POST['name'];
// instatiate Product
$product = Mage::getModel('catalog/product');
setSku($name.rand());
$product->setName("Free Sample Of ".$name);
$product->setDescription("Free Sample of ".$name);
$product->setShortDescription($name." - Sample");
$product->setPrice(00.01);
$product->setTypeId('simple');
$product->setAttributeSetId(3); // need to look this up
$product->setCategoryIds("0"); // need to look these up
$product->setWeight(0.000);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled
// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId())); 

// get product's general info such price, status, description
$stockData = $product->getStockData();

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

// then set product's stock data to update
$product->setStockData($stockData);

// call save() method to save your product with updated data
$product->save();
header("Location: /checkout/cart/add/product/".$product->getId()."/"); 

?>

To explain the above code:

- We pass the name attribute across via post

- We make a connection to magento

- We create a new product giving the SKU (as it needs to be unique) a name of productname+a random 10-digit number (e.g Isadore Floral White on Black 24.8×39.81418705824)

- We then set the price as 1p and the weight as 0

- We then fill out the other attributes and save the product

- We then use this new product and direct magento to the add to cart feature using the id of our brand new dynamically created product

3. Create a small form wherever you want the free sample button to be placed. I put it on the product list page (app>design>frontend>default>theme>catalog>product>view.phtml)

<form action="/getSample.php" method="post">

<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/freesample.gif') ?>" />

</form>

4. This form sends the product name (as that’s all the script needs) and creates the product – then sending the user to the shopping cart (as if they’ve added it to cart).

5. The last thing to do is then set the table rates for your shipping so that anything with a weight of exactly 0.000 has the required price.

This is by far not the cleanest method of doing a free sample but the fact that it works makes it worthwhile implementing. I’m always looking into cleaning up this code and perhaps making a module from it.

Thanks for visiting e-commerce website design and checking out our magento blog.

  • Twitter
  • AIM
  • WordPress
  • LinkedIn
  • Facebook
  • StumbleUpon
  • Technorati Favorites
  • Delicious
  • Ping
  • Digg
  • Bebo
  • Reddit
  • MySpace
  • NewsVine
  • Google Bookmarks
  • Sphinn
  • Propeller

6 Responses to “Magento Free Samples Solution”

  1. Leah Taylor

    Hi, thanks for this, this may be very useful for me. But I am just wondering, having read the solution, is there any reason the price can’t be set to £0.00, instead of £0.01? Wouldn’t this work just as well?

  2. Rob

    Hi Leah,

    The reason its set to £0.01 was because of the shipping method used on that particular website – it was price based. If it was free shipping for a sample then yes you are correct the £0.00 price would work just as well.

    But please try it out and let me know how it goes!

    Thanks for visiting,

    Rob

  3. Lorenzol

    Thanks for this beatiful solution. My problem is that the quantity is not set.

    $stockData['qty'] = 5;

    The quantity for all added products using this script ist still 0

    Coud you help

    Thanks

    Luis

  4. Rob

    Hi there luis,

    You could try adding $stockData['use_config_manage_stock'] = 0;
    This will override your default stock management and should force the qty to be added.

    Cheers,

    Rob

  5. Lorenzol

    Hi Rob that works thanks. I try to link simple produchts to an existing configurable product but i dont find a way how to do it. Lets say i have the id of the configurable product and all the iDs of the simple product that should be linked in a array

    ID_Configurable_product = 125;
    IDs_simple_product = array (22,55,88,125);

    Is there some example for this

    Thanks

    Luis

  6. Vicky

    I just tried this but when I click the button on the product page, the getSample.php file comes up in my browser but nothing else happens. Any ideas what I am doing wrong?

Post A Comment