Jump to content

Downloadable products question


Recommended Posts

It is the mails/iso_code/download_product.tpl that creates the links in the mails/iso_code/download_product.html email template. It is lines 133-147 of classes/OrderHistory.php that passes the links into the TPL file:

$assign = array();
foreach ($virtualProducts AS $key => $virtualProduct)
{
   $id_product_download = ProductDownload::getIdFromIdProduct($virtualProduct['product_id']);
   $product_download = new ProductDownload($id_product_download);
   $assign[$key]['name'] = $product_download->display_filename;
   $assign[$key]['link'] = $product_download->getTextLink(false, $virtualProduct['download_hash']);
   if ($virtualProduct['download_deadline'] != '0000-00-00 00:00:00')
       $assign[$key]['deadline'] = Tools::displayDate($virtualProduct['download_deadline'], $order->id_lang);
   if ($product_download->nb_downloadable != 0)
       $assign[$key]['downloadable'] = $product_download->nb_downloadable;
}
$smarty->assign('virtualProducts', $assign);
$iso = Language::getIsoById(intval($order->id_lang));
$links = $smarty->fetch(_PS_MAIL_DIR_.$iso.'/download-product.tpl');

Link to comment
Share on other sites

Ok, I need a "map" of how this downloadable product thing works because I can't wrap my head around it.

Customer adds product to Cart->Customer goes to checkout->
Customer selects PayPal/Google Checkout->Customer completes payment->
PayPal/Google Validates->Then what happens?

Our products are to be dynamically generated and I want to be able to generate them at the correct time for them to be
available for download. There is already an entry in ps_product_download for the item even though the file may not exist.
Does the filename have to match the display_name or physically_filename in the table?

Link to comment
Share on other sites

You should put the file in the download directory with the value of "physically_filename" as the filename. The "display_filename" is what the file is saved as when the download box pops up for the customer. The email will be sent after PayPal/Google Checkout returns "Successful". Hopefully, your download will be generated and saved in the download folder before the customer has a chance to read the email and click the link, otherwise an error message will pop up saying "This file no more exists."

Link to comment
Share on other sites

Yes, it is convoluted, but it has been done that way to prevent the file being downloaded by anyone but the intended recipient within the timeframe you specify. The 'updateOrderStatus' hook is called after the order status is changed to "Payment accepted" (or any other order state) before sending out email. The 'postUpdateOrderStatus' hook is called after the email has been sent. If you are writing a module, you could create a function using the 'updateOrderStatus' hook to execute code before the email is sent. For example, add the following function to your module:

function hookUpdateOrderStatus($params)
{
   // If the order status was changed to "Payment accepted"
   if ($params['newOrderStatus'] == 2)
   {
       // Generate downloadable file
   }
}

Link to comment
Share on other sites

So, do you think will this module code work? I butchered up the skeleton from Paul Campbell. I don't know if I took out anything critical. I don't need to configure anything so there is no need for a form (displayForm) and I'm not hooking it to any block so I don't need the other hooks.

<?php
class ReportGenerator extends Module
 function __construct()
 {
   $this->name = 'reportgenerator';    
   parent::__construct();

   $this->tab = '';
   $this->version = '1.0';
   $this->displayName = $this->l('A Prestashop Module to generator reports for PartReports.com');
   $this->description = $this->l('Enable this module to autogenerate the reports that customers order.');
 }

 public function install()
 {
   parent::install();
 }

 function hookUpdateOrderStatus($params)
 {
     // If the order status was changed to "Payment accepted"
     if ($params['newOrderStatus'] == 2)
     {
         /* Here I'm going to generate my downloadable file
          * 
          * First, check to see if there is a file in the downloads folder 
          * matching the physically_filename of the product(s) in the order.
          * 
          * If there is one, check to see if it is < 14 days old, if it is,
          * skip generation and the email will get sent out as normal.
          * 
          * If there is no file or one that is older than 14 days, call
          * build_report.php with the partno to generate the report.
          * 
          * Save the generated report with the physically_filename in 
          * the downloads directory. The email will then be sent containing 
          * that filename.
          * 
         */
     }
 } 
}
// End of: reportgeneration.php



What params are available in the function call?

I will need to know the orderID so I can get the product ID(s) to get the info I need to gen the report, it is going to be based on the value I stored in the reference column in ps_product.

In the comments above does that seem like the correct sequence?

Do the files in the downloads folder have the physically_filename as the full name of the file with no extensions?

hookUpdateOrderStatus happens on all payment methods, correct, so I don't have to make mods to my PayPal or GoogleCheckout modules.

Link to comment
Share on other sites

Ok, I think I may have it now. Know of any way to test this on localhost?

     $order = new Order(intval($params['id_order']));
     if ($order AND !Validate::isLoadedObject($order))
       die (Tools::displayError('Incorrect object Order in ReportGenerator Module.'));
     foreach ($order->getProducts() as $product) {
       $download = new ProductDownload(intval($product['id_product']));
       $filename = $download['physically_filename'];
       echo $product['reference']."
";
       echo $product['id_product']."
";
       echo $filename."
";
       $gen_report = false;
       if(file_exists($filename)) {
         $modified = filemtime($filename);
         $minus14  = mktime(0, 0, 0, date("m")  , date("d")-14, date("Y"));
         if($minus14 > $modified) {
           echo "File is older than 14 days, regen.
";
           $gen_report = true;
         }
       } else {
         echo "File doesn't exist, regen.
";
         $gen_report = true;
       } // if(file_exists($filename))
       if($gen_report) {
         // Call build_report.php to generate the report
       } // if($gen_report)
     } // foreach ($order->getProducts() as $product)

Link to comment
Share on other sites

Rocky,
I created an order and this hook never fired or at least I didn't see anything from the above code AND I didn't get the customer email about the download. The download is accessible from the order details page but I though it was supposed to send an email to the customer with a download link?

Link to comment
Share on other sites

That code looks fine, though you aren't installing the module into the "updateOrderStatus" hook in the install function.

The "download_product" e-mail should be sent. It's working fine on my site. Make sure that the addWithemail function in classes/OrderHistory.php is being called. Also, make sure you have "Send e-mail to customer when order is changed to this status" ticked for the "Payment accepted" order status (on the Orders > Statuses tab).

As long as the last order state is "Payment accepted" and $order->getVirtualProducts() returns at least one virtual product, the email should be sent.

Link to comment
Share on other sites

When is the download_product email supposed to be sent? After the Payment Accepted email?

I added this to function install() in my module

   if ($this->registerHook('updateOrderStatus') == false) {
         return (false);
   }



But it is still not sending the emails. Yes, I checked and it is set to send to customer.
I am still testing with PayPal sandbox and shouldn't sandbox user get the email in the
Test Emails area? It gets Receipts but that may be PP generated.

I have also been trying to enable FirePHP to do some debugging into OrderHistory class.
Having difficulties with that but that is another story, nothing gets sent even without FirePHP.

Link to comment
Share on other sites

Yes, it should be sent after the "Payment accepted" email. At least, that's the order they arrive on my website. I don't know why it isn't working on your site. Perhaps you have an email configuration problem if you are having trouble with the PayPal emails too.

Link to comment
Share on other sites

  • 3 years later...
×
×
  • Create New...