This is a widely known issue with the iPhone 7/7 Plus models, it has to do with the Audio IC which is a chip on the internal logic board that is prone to failure.
The only “fix” would be an experienced technician successfully jumping a pin on the chip using specialized micro-soldering workbench tools. This is not an issue you can throw parts at to repair.
Apple will not acknowledge the issue, but they will treat it as a whole-unit replacement when you bring the device to them. If you have AppleCare+ coverage on your device, you are covered.
I would not take this to a repair shop unless you absolutely trust the technician working on it. One slip-up during a job like this can render the phone completely useless as the tech can break plenty of other fragile components on the board if they are not careful. One workaround to the problem is using bluetooth headphones, if you do not want to spend the money to fix it.