Shopify Script, Buy Product from vendor X get Product Y for free

In shopify I was looking for a way to get a free product when buying from a specific vendor, I came up with below script.
In our case we also needed the vendor’s product to be at > $400 for the gift product to be free, you can offcourse remove that if statement.
Unfortunately you can’t add products to the cart with shopify scripts (yet?) so¬†you can leave adding the product up to the customer, or add the product with javascript in the frontend.

You can remove the puts statements, I though I’d leave them so you can see how to debug something like this.