Skip to content

Instantly share code, notes, and snippets.

@traviswaelbro
Last active December 18, 2015 17:33
Show Gist options
  • Save traviswaelbro/ddcd4adcc191a49277e4 to your computer and use it in GitHub Desktop.
Save traviswaelbro/ddcd4adcc191a49277e4 to your computer and use it in GitHub Desktop.
Magento file that allows adding to cart from external site's form data.
<html>
<body>
<p>Running</p>
<?php
include_once "app/Mage.php";
umask(0);
Mage::app();
Mage::getSingleton("core/session", array("name" => "frontend"));
echo "Sorry, it looks like something might have gone wrong! You might <a href='javascript:history.back()'>go back</a> and try again.<br />"; // In case a customer ends up here with an error preventing redirect
$cartProducts = array();
$productsArray = array();
$quantitiesArray = array();
$relQuantities = explode(',', $_POST['relQuantity']);
// URL Components for redirect
$source = $_POST['source'];
$medium = $_POST['medium'];
$campaign = $_POST['campaign'];
$variant = $_POST['variant'];
// Get all products & quantities from POST data
// Allows a variable number of products to be loaded into the file from an outside source.
$n = 1;
// echo "Product: ".$_POST['product'.$n]."<br/>";
// echo "Quantity: ".$_POST['quantity'.$n]."<br/>";
foreach($_POST as $k => $v) {
if(strpos($k, 'product') !== false) {
echo $k . " = " . $v . "<br/>";
array_push($productsArray, preg_replace('/[^A-z0-9-.,\/]/', '', $v) );
}
if(strpos($k, 'quantity') !== false) {
echo $k . " = " . $v . "<br/>";
array_push($quantitiesArray, preg_replace('/[^0-9]/', '', $v) );
}
}
// Apply the relative quantities throughout to get final quantities for all items
// Allows a 'kit' to have pre-defined quantity relations between its products and
// for a customer to say they want n times that kit.
$n = 0;
foreach ($relQuantities as $relQuantity) {
$quantitiesArray[$n] = $relQuantity * $quantitiesArray[$n];
$n++;
}
// Convert SKUs to IDs
$n = 0;
foreach ($productsArray as $product_sku) {
$cartProducts[$n] = Mage::getModel("catalog/product")->getIdBySku( $product_sku );
echo "Product SKU: ".$product_sku.", Product ID: ".$cartProducts[$n].", Quantity: ". $quantitiesArray[$n] ."<br />";
$n++;
}
// Add all products to cart
$n = 0;
$cart = Mage::getSingleton('checkout/cart')->init();
foreach($cartProducts as $product_id) {
if($quantitiesArray[$n] != 0) {
$product = Mage::getModel("catalog/product")->load($product_id);
if($product->isGrouped()) {
$super_group = array();
$children = $product->getTypeInstance(true)->getAssociatedProducts($product);
foreach($children as $child) {
$super_group[$child->getId()] = $child->getQty();
}
$params = array('super_group' => $super_group);
$repeat = $quantitiesArray[$n];
for($p = 0; $p < $repeat; $p++) {
$cart->addProduct($product, $params);
}
} else {
$cart->addProduct($product, $quantitiesArray[$n]);
$n++;
}
} else $n++;
}
foreach ($cart->getAll as $item) {
echo $item->getSku() . " x" . $item->getQty() . "<br>";
}
// Save Cart contents, update, and redirect to Cart Review page
$cart->save();
Mage::getSingleton('customer/session')->setCartWasUpdated(true);
Mage::getSingleton('core/session')->addSuccess('Products added to cart successfully. Enjoy!');
$goToUrl = Mage::getBaseUrl()."checkout/cart/" . "?source=" . $source . "&medium=" . $medium . "&variant=" . $variant;
Mage::app()->getResponse()->setRedirect($goToUrl)->sendResponse();
exit;
?>
<p>Done! If you're still here, try clicking this link: <a href="<?php echo $goToUrl; ?>">Go To Cart</a></p> <?php // In case a customer ends up here with an error preventing redirect ?>
</body>
</html>
@traviswaelbro
Copy link
Author

Updated version allows for variable number of products & quantities to be added, with relative quantity (to set kit relationships). With these options, we can add a single product, add multiple products, or add a kit (pre-defined quantities of certain items) with customer-chosen quantity of kits (respects pre-defined quantity relations).

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