Global variable doesnt’t pass to another function

So, if anyone has the very same problem, as I had – here is an answer.
Basically, before I write any kind of post with asking for help, I try to make sure, that I have used every solution, that I know, or have found on forums. BUT still, there is a chance, that I get something wrong, or use other combination.

And that was one of those times.
So anyway, global $sum inside post_orderfunction should be outside foreach loop. I have placed it just before loop. Then, in function update_custom_meta I didn’t post global $sum, now it’s fixed.

And that’s it! I thought, that i have tried those, but it turned out I didn’t. Hope it helps someone in the future 🙂