How to summon an immovable minecart?

Solution 1:

Invulnerable is easy; just summon it with the Invulnerable:1b tag. Immovable is a bit harder, as there is no built-in way to make minecarts immobile. Probably the easiest way is to summon the minecart as a passenger of another entity which is immobile and cannot be seen or interacted with, such as a lingering potion cloud.

summon area_effect_cloud ~ ~ ~ {Duration:-1,Age:-2147483648,WaitTime:-2147483648,Passengers:[{id:"minecraft:minecart",Invulnerable:1b}]}

This command will summon an area effect cloud that is completely invisible and does not apply any potion effect, and which will last for six real-life years (the maximum possible without constantly running commands to renew it). Riding the AEC will be your invulnerable minecart. Because it is riding the AEC, and the AEC cannot move, the minecart will not move.

Solution 2:

You can't prevent the minecart to be pushed, but you can teleport it back immediately:

Summon an immovable, invulnerable armor stand where you want the minecart to be:

/summon armor_stand ~ ~ ~ {NoAI:1,NoGravity:1,Marker:1,Invulnerable:1,Invisible:1,Tags:[minecartTpTarget]}

You won't see it unless you're in spectator mode.

Then put an active repeating command block either in the spawn chunks or near the armor stand with this command in it:

/tp @e[tag=minecartToTp] @e[tag=minecartTpTarget]

Then you summon the minecart with:

/summon minecart ~ ~ ~ {NoAI:1,NoGravity:1,Invulnerable:1,Tags:[minecartToTp]}