How to add a product with custom options into Magento shopping cart from an external site?

7 Comments

This is a follow up post to a previous article I wrote on how to add products into Magento but this time the script includes custom options (attributes) of the product. I used Magento version 1.4.0.1 in this example. Just copy the whole script and save it as index.php.

<?php
require_once ("/var/www/your-magento-directory/app/Mage.php");
umask(0);

// Initialize Magento
Mage::app("default");

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));

// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');

if(isset($_POST['submit'])){

	// call the Magento catalog/product model
	$product = Mage::getModel('catalog/product')
					 // set the current store ID
					 ->setStoreId(Mage::app()->getStore()->getId())
					 // load the product object
					 ->load($_POST['product']);

	// start adding the product
	// format: addProduct(<product id>, array(
	//         'qty' => <quantity>,
	//         'super_attribute' => array(<attribute id> => <option id>)
	//     )
	// )
	$cart->addProduct($product, array(
			'qty' => $_POST['qty'],
			'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
			)
		);

	// save the cart
	$cart->save();

	// very straightforward, set the cart as updated
	Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

	// redirect to index.php
	header("Location: index.php");

}else{
?>
<html>
<head></head>
<body>
<div style="width: 400px; margin: auto">
	<h3>Very Nice T-shirt</h3>
	<hr>
	<div style="width: 180px; margin:auto; line-height: 30px">
		<form action="index.php" method="post">
			Size:
			<select name="super_attribute[525]">
				<option value="">Choose an option...</option>
				<option value="100">Small</option>
				<option value="99">Medium</option>
				<option value="98">Large</option>
			</select><br>
			Quantity: <input type="text" name="qty" size="1" value="1">
			<input type="submit" name="submit" value="Add This">
			<input type="hidden" name="product" value="119">
		</form>
	</div>
	<hr>
	<strong><?php echo "Cart Qty: ".$cart->getItemsQty();?></strong>
</div>
</body>
</html>
<?php } ?>

7 Comments (+add yours?)

  1. Bill
    Jul 18, 2010 @ 21:10:57

    So how can we perform this within WordPress if the mage-enabler is plugged in.

    Reply

    • Richard Feraro
      Jul 18, 2010 @ 22:32:48

      You have to do the programming based on your need.

      Reply

  2. Scott Greenwald
    Jul 19, 2010 @ 15:07:28

    Very nice! I think this covers what Ive been looking for. Can we also pass form entries to the Magento cart?
    I would like to have a mini-form in wp that on submit auto populates the corresponding fields in onepage checkout, allowing users to complete the order. Is this possible?

    Reply

    • Richard Feraro
      Jul 19, 2010 @ 17:01:42

      Yeah it should allow you to do that. What are the fields that you plan to add from an external page?

      Reply

      • Scott Greenwald
        Jul 21, 2010 @ 00:09:28

        I have an external page where I’m selling one bottle of a weight loss product.
        The form consists of:
        First Name
        Last Name
        Address
        City
        State
        Zip
        Country
        Email
        Phone

        Submit would auto populate those fields in magento checkout, allowing users to complete the form and submit payment.

        Reply

        • Richard Feraro
          Jul 21, 2010 @ 15:48:23

          Yeah it is possible. Read it here.

        • Scott Greenwald
          Jul 22, 2010 @ 01:58:51

          Yeah, that’s all greek to me. Can I hire you to do it? Please send me an email if you’re interested

Leave a Reply