Magento adding downloadable product to cart by coding php
Probably you know it’s possible to add a product to the cart via querystring, but may be you prefer code a new module for your needs using Magento objects and methods.
So adding a simple product is easy:
... $cart = Mage::getSingleton('checkout/cart'); // start adding the product try { $cart->addProduct($product, array('qty' => 1)); // save the cart $cart->save(); $result = null; } catch (Mage_Core_Exception $e) { $result = $e->getMessage(); }
But how to add a Downloadable product with its right link ? Just try this, it’s a controller I used to integrate an “add to cart” API:
public function cartaddAction() { $productId = (int) $this->getRequest()->getParam('id'); $callback = (string) $this->getRequest()->getParam('callback'); $cart = Mage::getSingleton('checkout/cart'); // 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($productId); $links = Mage::getModel('downloadable/product_type')->getLinks( $product ); foreach ( $links as $link ) if ( preg_match("/epub/i", $link->getTitle()) ) $linkId = $link->getLinkId(); // Here is the trick to add the right link id $input = array( 'qty' => 1, 'links' => array( $linkId ) ); $request = new Varien_Object(); $request->setData($input); // start adding the product try { $cart->addProduct($product, $request); // save the cart $cart->save(); $result = null; } catch (Mage_Core_Exception $e) { $result = $e->getMessage(); } // very straightforward, set the cart as updated Mage::getSingleton('checkout/session')->setCartWasUpdated(true); }
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
17:45 on August 4th, 2012
Thanks a lot, we could definitely use this code for our music download site.
I wonder if you get protected urls ? or does the user see the real path of the source file ?
18:04 on May 17th, 2012
I am wondering if magento works easier than wordpress……….
Laser Hair Removal
13:43 on July 22nd, 2011
Great! this code is very beneficial for us.
14:08 on July 16th, 2011
Sure thing, this example looks promising, I think I should able to learn how to do this. Thanks for sharing!
19:50 on June 13th, 2011
Thats great. I was looking for a nice wordpress plugin to make a custom rss feed for my wordpress site. I was using xml sitemaps for quite sometime and this sounds even better. Thank you for the share :)
20:41 on May 4th, 2011
Concordo con tropea. A presto!
10:17 on May 3rd, 2011
I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts…Thanks
20:02 on April 28th, 2011
you’ve got a nice blog and i will request you to update the hyperlinks so that fascinated individuals can get all the info they want to have. Btw are you on twitter??
22:09 on April 27th, 2011
Concordo con villaggi
09:47 on April 25th, 2011
a very convenient portable solution
19:18 on April 22nd, 2011
hello blogger, i’d been reading your posts on Magento adding downloadable product to cart by coding php and i genuinely liked them. 1 thing that i observed whilst browsing throughout your blog that some of the links are not working and giving error. this makes the reading experience a bit bad. you’ve got a nice blog and i will request you to update the hyperlinks so that fascinated individuals can get all the info they want to have. Btw are you on twitter?? i would really like to follow you and get updates in your blog.
11:01 on April 21st, 2011
confermo
11:00 on April 21st, 2011
Confermo è stato molto utile grazie..
05:01 on April 15th, 2011
Which means I’d end up eating it…with a glass of wine in my hand.
10:25 on April 11th, 2011
Good one..Well it’s possible to add a product to the cart via querystring, but may be you prefer code a new module for your needs using Magento objects and methods.Thanks..
17:42 on March 23rd, 2011
Sono d’accordo con villaggi e grazie mille!
20:45 on March 21st, 2011
magento or virteumart?
21:56 on March 16th, 2011
Concordo, molto utile grazie :D
20:46 on March 15th, 2011
Magento works great, thanks for sharing working on it now.
10:02 on March 14th, 2011
May be you prefer code a new module for your needs using Magento objects and methods.