Feb 23, 2013
kalpesh

Magento: Product Free/Paid SAMPLE Purchase Order

If you want to allow your customers to order for sample of any product, before purchasing the actual product, it becomes very tough as in a product page you can’t have two products (sample + full) allowed. But it’s not a good idea to display sample products as Individually, it makes sense only in the actual product page just near the Add To Cart button.

Magento Sample product purchase

But the question is, how to have TWO SKUs in a single product detail page? Here is the code that will do exactly what you want now. Open your catalog/product/view.phtml template file and put this code just below the code “$this->getChildHtml(‘addtocart’);”

<button type="button" style="float:left;padding-left:5px;" onclick="window.location.href='/order_sample.php?sku=<?php echo $this->htmlEscape($_product->getSku()) ?>'" title="Request Sample for <?php echo $this->htmlEscape($_product->getName()) ?>" class="button btn-cart" ><span><span>Order Sample - $1</span></span></button>


And then create a php file “order_sample.php” in the root of your Magento installation:

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

$sku = $_GET['sku'];
if(!$sku) 
{
        header("Location: ".Mage::getStoreConfig('web/unsecure/base_url'));
        exit;
}
$prod = Mage::getModel('catalog/product');
$p = $prod->loadByAttribute('sku',$sku);

//validate if we have that SKU before creating it\'s sample..
if(!is_object($p) || strstr($sku, 'S-'))
{
        header("Location: ".Mage::getStoreConfig('web/unsecure/base_url'));
        exit;
}
$name = $p->getName();

//check if you already have the Sample product, then don\'t create new..
$p = $prod->loadByAttribute('sku','S-'.$sku);
if(is_object($p))
        $pId = $p->getId();
else
        $pId = false;

if(!$pId) {

$product = $prod;
$product->setSku('S-'.$sku); //Prepending "S-" before original SKU to prevent misunderstanding
$product->setName("Sample of ".$name);
$product->setDescription("Sample of ".$name);
$product->setShortDescription($name." - Sample");
$product->setPrice(1.00); //Price always $1
$product->setTypeId('simple');
$product->setAttributeSetId(4); // default - CHECK YOUR VALUE
$product->setCategoryIds("0"); // CHECK YOUR VALUE(s)
$product->setWeight(0.000);
$product->setTaxClassId(0); // none
$product->setVisibility(1); // not visible individually
$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'] = 1; //we're not managing stock anyhow
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 0; //don't manage sample stocks
$stockData['max_sale_qty'] = 1; //sample can't be purchased more than 1 qty

$product->setStockData($stockData);

$product->save();
$pId = $product->getId();
}

//redirect and add to cart page..
header("Location: /checkout/cart/add/product/".$pId."/");
exit;

Inspired from: http://opensourcetalking.com/magento/magento-free-samples-solution.html

8 Comments

  • do you have a working demo
    I cant get it to worrk

  • Hi, this code is not working in magento 1.8.Please reply soon.

  • Hi
    sample product is created but is not added to the basked
    can anybody help?

  • Thanks for this code, although I could not get it to work at first with 1.8 I have made some enhancements and modifications. It now works for 1.8 and product variations.

    https://gist.github.com/WazzaJB/8083094

  • Thanks for the code,

    On 1.7 it appears to be working fine as when you click the button, I receive a success message and product appears in my topcart, but when I proceed to Checkout, the product disappears and I return to an empty shopping cart.

    Would you have any suggestions on why this is?

    Many thanks

  • Hi Kalpesh, Nice Artcile !

    How can I make it to work only for sample products without actually buying anything from website? (ie cart only contains sample products). Also, In this case will the Buyer still have to go through checkout, I mean Buyer is simply ordering sample products worth $0.00 it will still go through Payment process. Any suggestions would be really help full.

  • Have installed the code but didt get cuple of thing ..
    first : how to control it ? it is not appeared in admin control so as in products
    second: when im clicking ad free sample i getting error on line 57 :
    Fatal error: Class ‘Mage’ not found in ////order_sample.php on line 57

    57: header(“Location: “.Mage::getStoreConfig(‘web/unsecure/base_url’));

    above code do not work at all with 1.8

  • add following code by remove line 63 and 64.

    Mage::getSingleton(‘core/session’, array(‘name’ => ‘frontend’));
    try {
    $product_id = $pId;
    $qty1 = ‘1’;
    $product = Mage::getModel(‘catalog/product’)->load($product_id);
    $cart = Mage::getModel(‘checkout/cart’);
    $cart->init();
    $cart->addProduct($product, array(‘qty’ => $qty1));
    $cart->save();
    Mage::getSingleton(‘checkout/session’)->setCartWasUpdated(true);
    Mage::getSingleton(‘core/session’)->addSuccess(‘Sample Product added’);
    header(‘Location: ‘ . ‘index.php/checkout/cart/’);
    } catch (Exception $e) {
    echo $e->getMessage();
    }

    It works for me.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions