Jump to content

[SOLVED] Handling errors with virtual products


MrBaseball34
 Share

Recommended Posts

We have dynamically generated reports that we sell. The user pays for the product and then the system
generates the report if the one currently stored in the download folder is older than 14 days or if it doesn't exist.

I have been doing great in my testing with reports that were generated. However, I am looking for a way to handle when the report does not get generated for any reason. There are several ways for the report to not be generated and the function ReportGenerator::BuildReport() will return false if it fails.

In OrderHistory->changeIdOrderState(), I have this code to generate my reports:

if(intval($new_order_state) == _PS_OS_PAYMENT_) {

 if(!file_exists($filename)) {
   /*********************************************
   *  If report file doesn't exist, generate a new one
   **********************************************/
   $gen_report = true;
 } else {
   $modified = filemtime($filename);
   $minus14  = mktime(0, 0, 0, date("m")  , date("d")-14, date("Y"));
   if($minus14 > $modified) {
     /*********************************************
     *  If report file is > 14 days old, generate a new one
     **********************************************/
     $gen_report = true;
   }
 } // if(file_exists($filename))
 if($gen_report) {
   /*********************************************
   *  ReportGenerator Class handles building of Report
   *  returns false if any problems encountered
   *
   *  I have deliberately put 
   *    return false;
   *  as the first line in the BuildReport function
   *  for testing...
   **********************************************/
   if(!ReportGenerator::BuildReport($filename, $partno)) {
     if($debug_log) { 
       Tools::DebugLog(__CLASS__." (".__LINE__.")", "Msg: Problem Building Report for:".$partno);
     }
     // Change the order state so it wil be known in addWithemail().
     $new_order_state = _PS_OS_GEN_ERROR_;
   } else {
     unset($rg);
   }
   // Report is now generated...                    
 } // if($gen_report)
} // if($this->id_order_state==_PS_OS_PAYMENT_)



Now, in PaymentModule->validateOrder(), we have this code:

$new_history = new OrderHistory();
$new_history->id_order = intval($order->id);
$new_history->changeIdOrderState(intval($id_order_state), intval($order->id));
if($new_history->id_order_state) == _PS_OS_GEN_ERROR_) {
 // Modified addWithemail function to add the last param
 // to allow me to change template if the report wasn't generated.
 $new_history->addWithemail(true, $extraVars, true);
 // Change it back to a valid good state.
 $new_history->id_order_state = _PS_OS_PAYMENT_; 
} else {
 $new_history->addWithemail(true, $extraVars);
}  

// We never have OOS items so this will never run!
if (isset($outOfStock) AND $outOfStock)
{
   $history = new OrderHistory();
   $history->id_order = intval($order->id);
   $history->changeIdOrderState(_PS_OS_OUTOFSTOCK_, intval($order->id));
   $history->addWithemail();
}



For some reason this isn't working. I never get an email NOR the debug message.
Any ideas as to why?

Share this post


Link to post
Share on other sites

What order status ID do you have _PS_OS_GEN_ERROR_ set to? When you go to Orders > Statuses and edit the order status with that ID, do you have "Send e-mail to customer when order is changed to this status" ticked and an appropriate mail template selected?

Share this post


Link to post
Share on other sites

I did not set things up like that. That is a good idea, though. However, I think the system still sends the confirmation email. I will look into setting things up like that. I created that status but forgot that it could be setup in the BO.

I was manually changing the statuses and trying to get the correct email to be sent. Let me try this, next to see if it works.

Thanks, Rocky.

Share this post


Link to post
Share on other sites

Ok, let me go through what I have done..
I moved my report generation code from OrderHistory->changeIdOrderState() to inside the PaymentModule->validateOrder() function.
Looks good except that it does not load and send the email with the download_product_error template.
I know that is the correct template because I set a debug message to test this value in OrderHistory->addWithemail():

$result['template']



Don't know where to debug next, unless I need to get deeper into the Mail class.

Share this post


Link to post
Share on other sites

Well, it certainly does help if you transfer ALL your files to your server, now doesn't it?
I did not transfer the templates and that was what was causing the whole bloody mess.

Anyway. I'm off to bigger and better things. Thanks for the heads up on the Order Statuses. :bug:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More