Add Sample Product on Magento Product Pages

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.   001 <?php 002 003 ini_set('display_errors', 1); 004 error_reporting(E_ALL); 005 006 //print_r($_POST); 007 008 require_once 'app/Mage.php'; 009 Mage::app(); 010 011 012 013 Mage::getSingleton("core/session", array("name" => "frontend")); 014 Mage::getSingleton('core/session')->setSampleError(""); 015 // Store users session 016 $session = Mage::getSingleton("customer/session"); 017 018 //Store cart items 019 $totalNumOfCartItem = Mage::getModel('checkout/cart')->getItemsQty(); 020 021 //Store cart items 022 $theitems = Mage::getModel('checkout/cart')->getItems(); 023 024 $thecount = 0; 025 026 foreach($theitems as $ti){ 027 028 $thename = $ti['name']; 029 030 $test = strpos($thename,'Free Sample'); 031 032 if($test !== false){ 033 $thecount = $thecount+1; 034 } 035 036 } 037 038 if(isset($_POST['name'])){ 039 $name = $_POST['name']; 040 041 $id = $_POST['id']; 042 if(isset($_POST['simpleid']) && !empty($_POST['simpleid'])){ 043 $simpleid = $_POST['simpleid']; 044 } 045 } 046 if(isset($_GET['name'])){ 047 $name = $_GET['name']; 048 if(isset($_GET['simpleid']) && !empty($_GET['simpleid'])){ 049 $simpleid = $_GET['simpleid']; 050 } 051 $id = $_GET['id']; 052 } 053 054 // retrict shopping cart to only allow 5 samples 055 if ($totalNumOfCartItem <= 4) 056 { 057 $theproduct = Mage::getModel('catalog/product')->load($id); 058 059 if($simpleid){ 060 $thesimple = Mage::getModel('catalog/product')->load($simpleid); 061 } 062 063 if($thecount < 2){ 064 065 // instatiate Product 066 $product = Mage::getModel('catalog/product'); 067 if($simpleid){ 068 $product->setSku($thesimple->getSku()."*".rand()); 069 } else { 070 $product->setSku($theproduct->getSku()."*".rand()); 071 } 072 $product->setName("Sample of ".$name); 073 // $product->setDescription("Sample of ".$name); 074 $product->setShortDescription($name." - Sample"); 075 $product->setPrice(00.00); 076 $product->setTypeId('simple'); 077 078 $product->setImageUrl($theproduct->getImageUrl()); // set the images from the original product 079 $product->setThumbnailUrl($theproduct->getImageUrl()); 080 $product->setImage($theproduct->getSmallImage()); 081 $product->setMediaGallery (array('images'=>array (), 'values'=>array ())); 082 $product->addImageToMediaGallery($_SERVER['DOCUMENT_ROOT']."/media/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false); 083 $product->setAttributeSetId(9); // need to look this up 084 $product->setCategoryIds("15"); // need to look these up 085 $product->setWeight(21212121.21); 086 $product->setTaxClassId(2); // taxable goods 087 $product->setVisibility(1); // catalog, search 088 $product->setStatus(1); // enabled 089 // assign product to the default website 090 $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId())); 091 092 093 // get product's general info such price, status, description 094 095 $stockData = $product->getStockData(); 096 097 // update stock data using new data 098 $stockData['qty'] = 1; 099 $stockData['is_in_stock'] = 1; 100 $stockData['manage_stock'] = 1; 101 $stockData['max_sale_qty'] = 5; 102 103 // then set product's stock data to update 104 $product->setStockData($stockData); 105 //die(); 106 107 // call save() method to save your product with updated data 108 $product->save(); 109 110 111 header("Location: /checkout/cart/add/product/".$product->getId()."/"); 112 } 113 } 114 115 else 116 { 117 Mage::getSingleton('checkout/session')->addError("You are only allowed a maximum of 5 sample products"); 118 119 header("Location: /checkout/cart"); 120 } 121 ?> 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.   1 <form action="/getSample.php" method="post"> 2 <input type="hidden" name="id" value="<?php echo $_product->getId() ?>" /> 3 <input type="hidden" name="name" value="<?php echo $this->htmlEscape($_product->getName()) ?>" /> 4 <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') ?>" /> 5 </form> 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.

Site Search

Contact Us

 
 
 
Parent Technology Inc.
1535 Rosalind Rd Se
East Grand Rapids, MI 49506, USA
 
 

This email address is being protected from spambots. You need JavaScript enabled to view it.