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);
}
Tags: , , , , , , , ,

Post simili:

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.

21 Responses to “Magento adding downloadable product to cart by coding php”
  1. Elevator Music
    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 ?

  2. Laser hair removal
    18:04 on May 17th, 2012

    I am wondering if magento works easier than wordpress……….

    Laser Hair Removal

  3. Furniture
    13:43 on July 22nd, 2011

    Great! this code is very beneficial for us.

  4. Ash
    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!

  5. Free jar games
    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 :)

  6. vacanze capo vaticano
    20:41 on May 4th, 2011

    Concordo con tropea. A presto!

  7. Web Visible
    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

  8. HMS Home Warranty
    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??

  9. inglese
    22:09 on April 27th, 2011

    Concordo con villaggi

  10. mbt shoes australia
    09:47 on April 25th, 2011

    a very convenient portable solution

  11. Carson Griffin
    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.

  12. capo vaticano
    11:01 on April 21st, 2011

    confermo

  13. vacanze capo vaticano
    11:00 on April 21st, 2011

    Confermo è stato molto utile grazie..

  14. five fingers
    05:01 on April 15th, 2011

    Which means I’d end up eating it…with a glass of wine in my hand.

  15. Max Furniture
    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..

  16. traduzioni
    17:42 on March 23rd, 2011

    Sono d’accordo con villaggi e grazie mille!

  17. Jamon iberico
    20:45 on March 21st, 2011

    magento or virteumart?

  18. ricadi
    21:56 on March 16th, 2011

    Concordo, molto utile grazie :D

  19. Laptop Troubleshooting
    20:46 on March 15th, 2011

    Magento works great, thanks for sharing working on it now.

  20. LocalPosition.com
    10:02 on March 14th, 2011

    May be you prefer code a new module for your needs using Magento objects and methods.

Leave a comment

(required)

(required)


*

Categories