The response is as follows:
The command to give charity was not an obligation imposed upon every Muslim, such that they would be sinners for failing to give it. Rather, only someone wishing to consult the Messenger privately was commanded to do so. It so happened that, at that time, no one wished to consult him privately except Ali ibn Abī Tālib—may Allah be pleased with him—and so he gave charity for the purpose of that private consultation(1).
This is like Allah’s command that anyone who performs the minor pilgrimage before Hajj offer a sacrificial animal; His command that anyone prevented from completing the pilgrimage offer a sacrificial animal; and His command that anyone suffering an ailment of the scalp make compensation by fasting, giving charity, or making a sacrifice. This verse was revealed concerning Ka‘b ibn Ujrah, when the Prophet—may Allah bless him and grant him peace—passed by him while he was blowing on the fire beneath a pot and the lice on his head were afflicting him(2). It is also like His command that anyone who is ill or on a journey complete an equal number on other days; His command that anyone who breaks an oath feed ten needy people, clothe them, or free a slave; His command that, when they rise for prayer, they wash their faces and their hands up to the elbows; and His command that, when they recite the Quran, they seek refuge in Allah from the accursed Satan. There are many similar examples.
Thus, when a command is made contingent upon a condition and that condition is met only in the case of one person, no one else is commanded