Does the first sentence have the same meaning as the second?
He put his hand in his pocket and bought it for the guy.
I'll have to put my hand in my pocket to fix my car.
Do these sentences have the same meaning:
to spend money, or to give some to someone or something?
And if the first sentence is incorrect, how I should write it?
Solution 1:
Put one's hand in one's pocket is an informal way of referring to spending money. It doesn't mean 'give something to someone', although of course that could be the reason for the expense. Your sentence is OK as a colloquial way of saying that one man decided to pay for something for another man.
Solution 2:
'Putting one's hand in one's pocket' is a common phrase that means spending one's own money but only as long as the phrase fits contextually. Also, this phrase doesn't mean you're giving someone something but you're spending something for yourself or others.
Your two sentences...
He put his hand in his pocket and bought it for the guy.
I'll have to put my hand in my pocket to fix my car.
talk about spending something, so they're okay. However, they could use more clarity if they're standalone sentences, such as what did he buy for the guy, or what part of the car needs fixing.