How to add a product into Magento shopping cart from an external site?
Apr 27
Feeling guru, Techie Daw e-commerce, Magento, open source, php, programming 15 Comments
Yes, I know. This topic has already been discussed here. But it seems the article isn’t really helpful for those people who would integrate Magento into a custom website because it will redirect the page to Magento’s default web store provided in the installation. The technique I’ll discuss here assumes that you know HTML and ofcourse familiar with Object Oriented Programming (OOP) in PHP.
Let’s start.
We begin our index.php page with a HTML form having one (1) textbox for quantity and a submit button.
<form action="process.php"> Product name: T-shirt <br> Quantity: <input type="text" name="qty[<?=$productID?>]"> <input type="submit" value="Add to Cart"> </form>
where $productID is the ID of the product that you want to add into your shopping cart and process.php is the file that will do the processing of the form. Also upon submission of the form, the qty field will pass the value into an array format similar to one below:
"qty" = array(12345 => 1)
where
qty is the field name
12345 is the product ID and
1 is the qty value
This will be the content of process.php file:
Include the Mage.php file on top of the process.php. Click here to see how.
Then proceed with the php script below:
// continuation of process.php
$items = $_POST["qty"];
// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');
foreach($items as $key => $value)
{
// 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($key);
// start adding the product
$cart->addProduct($product, array('qty' => $value));
// 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");
That’s it! I didn’t add the end tag for php since it isn’t required for pure php file. I included alot of comments within the code to explain how each line works. If it causes you some problems, just remove the comments
RSS
Facebook
Friendster
LinkedIn
Follow me on Twitter
Jul 22, 2009 @ 05:00:33
Do you have any idea to open product detail page using product id?
Jul 22, 2009 @ 09:48:18
Yes. It is done using the code below:
$mage = Mage::getSingleton('catalog/product'); $product = $mage->load($product_id);using $product you can now access the details of the specified id. For example, you can view all the details by running the code below:
Oct 29, 2009 @ 04:31:30
And how would you remove the product
I tied:
$cart = Mage::getSingleton(‘checkout/cart’);
$cart->removeItem($item_id); //item_id not product_id
$cart->save();
Mage::getSingleton(‘checkout/session’)->setCartWasUpdated(true);
but it did not work
btw thanks for your article it already helped me a LOT!
cheers!
Oct 29, 2009 @ 21:55:30
It’s quite easy
Set the quantity to 0
Thanks :p
Nov 24, 2009 @ 05:14:50
Hi !
How to add bundle with option ?
Thanks
Nov 29, 2009 @ 11:09:57
Any idea how to add a configurable product via URL?
I tried adding the configurable option IDs as in your methos but they seem to not catch.
Thanks
Dec 21, 2009 @ 14:41:02
I have the cookie location set for ‘/’ and it doesn’t work. I’m using latest version of Magento. I’ve tried countless variations of this script gleaned from the internet and it does not work. What location is the script running from? Does that matter? Did this functionality disappear in a recent upgrade?
I can not get into a Magento session from a script in a different folder on the same server for the life of me. Very frustrating.
I’m trying to bridge Magento and SMF and I have the Magento side down and done, but connecting TO Magento from SMF is just not working…
Anyone know how to accomplish this besides API?
thanks
Mar 03, 2010 @ 22:17:11
alex, are you making sure you have this being called on your page?
Mar 13, 2010 @ 07:56:48
Hi, great post.
I’m currently going through so that when a certain product is added to the cart and they are from such and such location then it adds a associated fee.
I’ve got it set up so when they checkout it’s adding the fee as a product kind of like what you’ve done above.
115,
'qty' => $qty,
);
$product = new Mage_Catalog_Model_Product();
$product->load(115);
$cart->addProduct($product, $params);
$cart->save();
?>
This works awesome, but my trouble is if they refresh the page or go away and come back to the page it adds the product again. Do you knowhow I could do some sort of product checking so if it’s been added it can’t be added again, or how to set the qty after the cart->save function?
Let me know, thanks.
Mar 13, 2010 @ 14:51:10
Thanks Ben!
I think it will behave that way unless you’ll do a couple of checking to your cart value. Refreshing the page will always add the product in the cart as long as your valid get/post values are still present which makes the $cart->addProduct() function to return TRUE. There are several ways you can prevent same data from being used more than once but I think these are the most common:
1. Creating a separate confirmation page for successful/failed add to cart request, or;
2. Do the validation on the same page.
If you choose the last option, you can check if the productid is already present in the cart. If it is, then you can get the current qty and add your new qty value to it. Removing a product in the cart also works this way because setting the qty to zero deletes the item from the cart.
You can experiment using the functions getProductIds and updateItems found in the Magento Documentation
Mar 24, 2010 @ 01:58:50
Removing an item with qty to 0 does not work. instead use this:
$items = $cart->getItems();
foreach ($items as $item) {
if ($product->getId() == $product_id) {
$itemId = $item->getItemId();
$cart->removeItem($itemId);
Mage::getSingleton(‘checkout/session’)->setCartWasUpdated(true);
break;
}
}
Mar 24, 2010 @ 02:02:06
Sorry, more information for above:
The product id for adding a product is not the same as the id for removing.
- Iterate through the cart items
- Get the product id and compare with the product id you want to remove.
- Remove the product
- Break out of the loop when product is found and deleted.
Missing a $cart->save(); in the above code.
Best regards,
John.
Jun 04, 2010 @ 15:10:24
Hi!
Great post, it help me a lot!
However, I have a question! How do I add a product with customs options?
For exemple a t-shirt with different color and size?
Thanks in advance for your answer
Gauthier
Jun 04, 2010 @ 17:54:29
Hello Gauthier,
Thanks for reading my blog. Regarding your question, please check my follow up post regarding the matter here
Keep on reading!
Richard