It is narrated from the Prophet ﷺ through the chain of Amr bin Dinar in a similar manner, and in this narration, these additional words are mentioned: That woman said, "O Messenger of Allah! Will this (Hajj on behalf of another) benefit him?" The Prophet ﷺ replied, "Yes, just as if he had a debt and you paid it off on his behalf, it would benefit him; in the same way, Hajj will also benefit him."
Hadith Referenceمسند الإمام الشافعي / كتاب الحج / 928
Hadith Takhrijصحیح موصولا اخرجه البيهقي : 4 / 328، 5/ 179 - وصححه ابن خزيمة (3042) ۔ وانظر الحديث السابق برقم : (927)۔