Hello,
When you process some custom, or update or modify in general your website, it's better to use a an unpublished website clone just for test purpose, it's a good way to make evolve your site without impact your sales.
Now, about the custom override view, we can guide you in the job, but we can't analyze custom code or provide sample code, because it's too long.
For this time only, I analyze your code, and can't see why this leads to an error, especially without seeing error message.
If you want our advice and allow us to guide you, details what you want to achieve, in which context (view) and all elements to allow us to better understand your goal with this custom.
Regards
Moderator : Please, in order to keep the forum clear for others, and don't mess our ticket process, don't edit your message a long time after have posted it, rather post a new message, thanks for your attention.