The fix should work when the category filer isn't selected now (pretty much just copy+paste with the category part removed)
Removed the category sections in the SQL in some of the if-else block
Also changed some of the SQL to use LEFT OUTER JOIN since it wouldn't show users who had 0 purchases (of everything) when category filter was selected.