Skip Checkout in Magento for a downloadable product
This code will allow logged-in customers to "place an order" for a Free, Virtual product while bypassing checkout and redirect them straight to the My Downloads section of their account.
Add the following line to your catalog/product/list.phtml
in the spot you want it.
<?php if($_product->isVirtual() && $_product->getFinalPrice()==0) { ?>
<a href="/modulename/checkout/purchase/id/<?php echo $_product->getId()?>"><?php echo $this->__('Download and Install') ?></a>
<?php } ?>
Then create a new module with a controllers/CheckoutController.php
containing this code:
public function purchaseAction() {
if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->_redirectUrl(Mage::getBaseUrl().'customer/account');
return;
}
$request = $this->getRequest();
$id = $request->getParam('id');
$product = Mage::getModel('catalog/product')
->load($id)
->setStoreId(Mage::app()->getStore()->getId());
if(!($product->getIsVirtual() && $product->getFinalPrice() == 0)){
Mage::getSingleton('checkout/session')->addError($this->__('Method only available for Free Downloadable Items'));
return $this;
}
$onepage = Mage::getSingleton('checkout/type_onepage');
/* @var $onepage Mage_Checkout_Model_Type_Onepage */
try{
$quote = $onepage->getQuote();
/* @var $quote Mage_Sales_Model_Quote */
$quote->addProduct($product);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$onepage->initCheckout();
$payment=array('method'=>'free');
$onepage->savePayment($payment);
$onepage->saveOrder();
$this->getResponse()->setRedirect('/downloadable/customer/products');
}
catch(Exception $e){
$result = $e->getMessage();
Mage::getSingleton('checkout/session')->addError($result);
}
}
You'll need to handle the Exceptions a little better, but that should be functionally correct.
Magento: Track number of downloads of downloadable product links
Look at the number_of_downloads_bought and number_of_downloads_used in downloadable_link_purchased_item table (Mage_Downloadable_Model_Link_Purchased_Item model)
Magento uses those fields to define how many times user can download file:
$linkPurchased = Mage::getModel('downloadable/link_purchased')->load($linkPurchasedItem->getPurchasedId());
$downloadsLeft = $linkPurchasedItem->getNumberOfDownloadsBought()
- $linkPurchasedItem->getNumberOfDownloadsUsed();
So you can use $linkPurchasedItem->getNumberOfDownloadsUsed() as number of downloads in selected order. If you count number of doewnloads for every order you can get total number of downloads.
Or you can make your own counter for calling Mage_Downloadable_DownloadController::_processDownload()
Related Topics
Best Way to Determine If a File Is Empty (Php)
How to Detect the Screen Dpi Using JavaScript
How to Insert into MySQL Using a Prepared Statement with PHP
How to Read Xml File from Url Using PHP
Symfony 2 Load Different Template Depending on User Agent Properties
How to Set Curlopt_Cainfo Globally for PHP
Utf-8 in PHP Regular Expressions
Change Tag Attribute Value with PHP Domdocument
Imagecreatefromjpeg and Similar Functions Are Not Working in PHP
Get a PHP Datetime Difference in Days, Considering Midnight as a Day Change
PHP Call Class Method/Function
PHP - Detect Whitespace Between Strings
Laravel Says "Route Not Defined"
Using Xpath with PHP to Parse HTML
How to Verify a Tls Smtp Certificate Is Valid in PHP