Skip to content

Instantly share code, notes, and snippets.

@WazzaJB
Last active January 8, 2019 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WazzaJB/8083094 to your computer and use it in GitHub Desktop.
Save WazzaJB/8083094 to your computer and use it in GitHub Desktop.
This will allow you to add the ability to order samples in Magento - and what better, it is the first version of the code I have found which works in 1.8!
<?php
/**
* WJB - Order Samples in Magento 1.8
*
* Upload this file to the root of your Magento installation and then you will
* need to add the following snippet to your 'template/catalog/product/view.phtml'
*
* You may need to change some of the values below in the sample creation code
* but this is all commented so it should be simple enough - my advice would be
* to read through and edit where necessary
<input type="hidden" name="sample-sku" value="<?php echo $this->htmlEscape($_product->getSku()) ?>" />
<button type="button"
onclick="sampleAddToCart(this)"
class="lwd-button lwd-button-sample">
<?php echo $this->__('Order Sample'); ?>
</button>
<script type="text/javascript">
function sampleAddToCart(sample)
{
var do_continue = true;
var sku = jQuery(sample).data('sku');
jQuery('.required-entry[name^="super_attribute"]').each( function() {
if( !jQuery(this).val() )
{
jQuery(this).addClass('validation-failed');
jQuery(this).after('<div class="validation-advice" id="advice-required-entry-attribute">This is a required field.</div>');
do_continue = false;
}
});
if( do_continue )
{
console.log('lets go');
jQuery('#product_addtocart_form')
.attr('action', '/orderSample.php' )
.submit();
}
return false;
}
</script>
*/
//////////////////////////////////////////////////////
// REQUIRE MAGE APP FOR ACCESSING MAGENTO FUNCTIONS //
//////////////////////////////////////////////////////
require_once 'app/Mage.php';
umask(0);
Mage::app(0);
///////////////////////////////////
// STORE GIVEN SKU IN A VARIABLE //
///////////////////////////////////
$sku = $_POST['sample-sku'];
//////////////////////////////////////////////
// IF NO SKU IS FOUND THEN REDIRECT US HOME //
//////////////////////////////////////////////
if (!$sku) {
header("Location: ".Mage::getStoreConfig('web/unsecure/base_url'));
exit;
}
///////////////////////////////////////////////////////////////////////
// LOAD USERS CURRENT SESSION - THIS FIXES THE SAMPLE SYSTEM FOR 1.8 //
///////////////////////////////////////////////////////////////////////
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
////////////////////////////////////////
// GET THE FULL PRODUCT FROM IT'S SKU //
////////////////////////////////////////
$prod = Mage::getModel('catalog/product');
$p = $prod->loadByAttribute('sku', $sku);
///////////////////////////////////////////////////////////
// LOADS THE CORRECT VARIANT IF A SUPER ATTRIBUTE IS SET //
///////////////////////////////////////////////////////////
if( isset($_POST['super_attribute']) )
{
$the_product = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($_POST['super_attribute'], $p);
if ( is_object($the_product) )
{
$p = $the_product;
$sku = $p->getSku();
}
}
//////////////////////////////////////////////////
// IF THE PRODUCT IS NOT AN OBJECT THEN GO HOME //
//////////////////////////////////////////////////
if ( !is_object($p) ) {
header("Location: ".Mage::getStoreConfig('web/unsecure/base_url'));
exit;
}
/////////////////////////////////////////////////////////////////////
// STORE ORIGINAL PRODUCTS NAME IN CASE WE NEED TO CREATE A SAMPLE //
/////////////////////////////////////////////////////////////////////
$name = $p->getName();
////////////////////////////////////
// SEE IF A SAMPLE ALREADY EXISTS //
////////////////////////////////////
$p = $prod->loadByAttribute('sku', 'S-' . $sku);
if ( is_object($p) ){ $pId = $p->getId(); }
else { $pId = false; }
if ( !$pId )
{
$product = $prod;
$product->setSku('S-' . $sku); // PREPEND 'S-' TO SKU TO MAKE IT CLEAR THIS IS A SAMPLE
$product->setName("Sample of " . $name); // SET THE NAME
$product->setDescription("Sample of " . $name); // SET THE DESCRIPTION
$product->setShortDescription($name . " - Sample"); // SET THE SHORT DESCRIPTION
$product->setPrice(1.00); // SET THE PRICE TO 1.00 - YOU MAY WANT TO CHANGE THIS
$product->setTypeId('simple'); // WE WANT THIS TO BE A SIMPLE PRODUCT
$product->setAttributeSetId(0); // THIS NEEDS CHANGING - IN MY CASE IT WAS 4
$product->setCategoryIds("0"); // CHECK YOUR VALUE(s)
$product->setWeight(0.000); // SET THE SAMPLES WEIGHT HERE IF YOU HAVE WEIGHT BASED SHIPPING
$product->setTaxClassId(0); // SET THE TAX GROUP
$product->setVisibility(1); // nOT VISIBLE INDIVIDUALLY
$product->setStatus(1); // ENABLE THE PRODUCT
// ASSIGN PRODUCT TO THE DEFAULT WEBSITE
$product->setWebsiteIds(array(
Mage::app()->getStore(true)->getWebsite()->getId()
));
// GET THE STOCK INFORMATION
$stockData = $product->getStockData();
// UPDATE STOCK DATA USING NEW DATA
$stockData['qty'] = 1; // SET STOCK TO 1
$stockData['is_in_stock'] = 1; // SET PRODUCT TO IN STOCK
$stockData['manage_stock'] = 0; // DON'T MANAGE THE STOCK OF SAMPLES
$stockData['max_sale_qty'] = 1; // ALLOW FOR ONLY ONE OF EACH SAMPLE IN CART
// SET THE STOCK INFORMATION
$product->setStockData($stockData);
// SAVE/CREATE THE PRODUCT
$product->save();
// GET THE PRODUCT ID
$pId = $product->getId();
}
//////////////////////////////
// LOAD THE PRODUCT FROM ID //
//////////////////////////////
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($pId);
////////////////////////////////////////////////////////////////////
// REDIRECT THE USER TO THE CART WITH THE CORRECTLY GENERATED URL //
////////////////////////////////////////////////////////////////////
header("Location: ".Mage::helper('checkout/cart')->getAddUrl($product).'?product='.$pId.'&qty=1' );
exit;
@kashu9028
Copy link

@YaegerDesign can you please guide how did you added thumbnail image of the product to the cart?

I have tried a lot But i am not able to show product thumbnail image in the cart.Can you please help?

@KirbyFox
Copy link

KirbyFox commented Jan 8, 2019

@malikabid Did you manage to resolved your SQL error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment