-
-
Save WazzaJB/8083094 to your computer and use it in GitHub Desktop.
<?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; |
Works like a charm in CE 1.9! The only thing I would add is to copy the product thumbnail over to the sample so it shows in the cart. Also, if you try to add the same sample twice, Magento redirects to the product view of the sample, which doesn't exist since the product is not directly visible, in order to give the error that only can be ordered.
@YaegerDesign:
Did you managed to add the thumbnail?
For redirect to product view i changed the end of the file to check if product already in cart:
//////////////////////////////////////////////////////////////////// // REDIRECT THE USER TO THE CART WITH THE CORRECTLY GENERATED URL // //////////////////////////////////////////////////////////////////// $quote = Mage::getSingleton('checkout/session')->getQuote(); if ($quote->hasProductId($pId)) { header("Location: ".Mage::helper('checkout/cart')->getCartUrl()); } else { header("Location: ".Mage::helper('checkout/cart')->getAddUrl($product).'?product='.$pId.'&qty=1' ); } exit;
@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?
@malikabid Did you manage to resolved your SQL error?
this works for me !! Thanks 😄