Yes foreigners can purchase a property in Spain. In order to do that, they need a NIE. Read more in our article: the 9 steps to your Spanish property