[Magento] Add downloadable products
来源:互联网 发布:wish for mac 编辑:程序博客网 时间:2024/05/17 04:44
Probably you know it’s possible to add a product to the cart via querystring, but may be youprefer code a new module for your needs using Magento objects andmethods.
So adding a simple product is easy:
...$cart = Mage::getSingleton('checkout/cart');// start adding the producttry {$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? Justtry 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 producttry {$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 updatedMage::getSingleton('checkout/session')->setCartWasUpdated(true);}
- [Magento] Add downloadable products
- Magento add most popular products
- Bestseller products in Magento
- magento -- 推荐插件 -- Featured Products
- How to Import Products into Magento
- How to import products into Magento?
- Magento 修改首页 New Products 列数
- Add Highslide to Magento
- magento add admin account
- magento导入导出Custom Options, Tier Prices and Grouped Products
- Display sub-category products in root category in Magento
- Do not show out of stock products in Magento
- Magento -- 让Magento系统目录只显示有库存的产品(Instock Products Only)
- Downloadable NLG systems
- Product Tier Price add/update in Magento
- Add Product Images via CSV in Magento
- magento add custom column to order grid
- [Magento] Add custom attribute by SQL
- Zend Framework: view and table
- PHP programming
- CMS Series:Joomla and Drupal
- [Magento] Setting PayPal Express checkout
- Develop your own Magento mobile application
- [Magento] Add downloadable products
- Dojo concept for javascript developer
- Programming Tips Summary
- Magento: Get category name
- Retrieving product information in Magento
- How to overload controller in Magento?
- [Magento] Overriding Core files
- Know more about your customer…
- How to create custom price in Magento?