The setting of all stories is important. For the Gift of the Magi it is particularly important. There are two main reasons for this.
First, what makes the desire for the exchange of gifts natural in the short story is that it is Christmas. This is the setting. Jim and Della are very much in love and they want to get each other meaningful gifts for Christmas. More importantly, they want to express their love for each other. From this perspective, they underline the true meaning of Christmas.
Second, we can say that O. Henry depicts their poverty during this Christmas season. This makes the story even more moving, because both Jim and Della are giving in their poverty not out of their excess. This is how the story opens:
"One dollar and eighty-seven cents. That was all. And sixty cents of it was in pennies. Pennies saved one and two at a time by bulldozing the grocer and the vegetable man and the butcher until one's cheeks burned with the silent imputation of parsimony that such close dealing implied."
These opening words set the stage of the short story and the culmination ends with lavish giving at one's own expense.