Are you setting the products through the custom fields?
Something like could work – but only if $link was an array really, so if you have like the custom field array, with the key that maybe all the stuff is in – then you could put that in. It should then loop through the array and only output each one as long as there is one.
Or if that dosn’t work…
I hope that gives you an idea anyway. Hard to figure it out without more info really