Download Link Not Showing for Programmatically Added Downloadable Product in Order

downloadablemagento-1.8orders

I have been tasked for creating a order for a downloadable product and placing a order via Paypal and fortunately I've done the task pretty well.

But when the customer get his order email the download link for the product is not showing.

Below in my code that I've used :

<?php
if(!isset($_GET['p'])){
    $msg = '<h2>Wrong data passed!!</h2>';
}
else{
$product = Mage::getModel('catalog/product')->load($_GET['p']);
$cancel_url = Mage::getURL('').'paypal/standard/cancel/';
$return_url = Mage::getURL('').'paypal/standard/success/';
$notify_url = Mage::getURL('').'paypal/ipn/';

if(Mage::getStoreConfig('payment/paypal_standard/sandbox_flag')=='1'){
    $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?';
}
else{
    $paypal_url = 'https://www.paypal.com/cgi-bin/webscr?';
}
$price = number_format((float)$product->getPrice(),2,'.',''); 
$quote = Mage::getModel('sales/quote');

$quote->setCustomerEmail('magegurusit@gmail.com');

$quote->setStore(Mage::app()->getStore());   

$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product);
$quoteItem->setQuote($quote);
$quoteItem->setQty(1);
$quote->addItem($quoteItem);

$addressData = array(
'firstname' => 'Guest',
'lastname' => 'Customer',
'street' => 'Some address on the earth',
'city' => 'Somewhere',
'postcode' => '123456',
'telephone' => '123456',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table,
'use_for_shipping'=>1
);

$billingAddress = $quote->getBillingAddress()->addData($addressData);
/*$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('paypal_standard');*/

$quote->getPayment()->importData(array('method'=>'paypal_standard'));
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
try {
    $service->submitAll();
    $order = $service->getOrder();
    $invoice = $order->getIncrementId();
} catch (Exception $e){   
    Mage::log($e->getMessage());
}

Order is creating properly.
Don't know what little extra I'm missing in the above.
Please help me on this.
Thanks

Best Answer

the download link for the product is not showing

This could mean the link as a whole does not show up, the link target is empty or it is not generated at all (no link_hash value) in the database.

The link to the downloadable Product gets generated within the sales_order_item_save_commit_after event in the saveDownloadableOrderItem method in Mage_Downloadable_Model_Observer.

So if it is not generated at all, have a look in your downloadable_link_purchased_item database table and check if there are values under link_hash and link_url or link_file fields.

If not, you might have another problem, with your e-mail template maybe.