2

I have a custom function that creates a PDF file, which is triggered for the event sales_order_save_after. But why this function is triggered twice, I got 2 PDFs everytime for the same order.

If I use sales_order_place_after, then the function is only triggered once. So this fix my problem.

But my question is why sales_order_save_after is triggered twice in Magento?

3

It's possible one of your Payment modules or something may be saving the order as well, causing the twice save.

I've solved this personally in the following way (you could use it in your pdf generator, assuming it's in observer?

class Your_Module_Model_Observer
{

   public $eventComplete = false;

   function sales_quote_collect_totals_after()
   {

    if($this->eventComplete){return;}

    $this->eventComplete = true;

    // pdf code here
    }
}
  • Thanks, I tried your code, and the event is triggered once. but I also fixed this by using the event sales_order_place_after, which is also triggered only once, as I mentioned in the question. It's the same right? – Magento Learner Aug 25 '17 at 13:49
  • Yeah, I beleive you can do things like $order->save(); in code, for example you load the order, change shipping then save it. Then load again, add payment, save it. Place is when the order has been literally placed so different from saves. – Ricky Odin Matthews Aug 25 '17 at 14:04
  • I actually noticed that when the event sales_order_save_after triggered twice, the two PDFs are not the same. the first one doesn't include the customer note (order comment) where the second one include the customer note. Is it possible to only get the second one with the customer note? with sales_order_place_after or your code above, I can only get the first PDF without customer note. – Magento Learner Aug 25 '17 at 15:13
  • Hmm, maybe try declaring $eventComplete as 0, then increment it in the function $this->eventComplete++; then you can check for value of it, e.g. if not == 1 then return false. – Ricky Odin Matthews Aug 30 '17 at 12:05
  • 1
    I confirm that the same problem exists in Magento 2 and this solution solved it for me. – Hassan Al-Jeshi Mar 21 '18 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.