Custom digital products & email delivery

Here’s the scenario…

Our client has a catalogue of 1,000s of ebooks that they want to sell on their site. We could create Commerce products for each but it seems overkill as they already exist on a database and would be purchased from dynamically served resources which are already linked up to the database. I’m planning on using the digital products module, but can this handle dynamic products, ie not added as a Commerce product directly.

Next question - how easy would it be to attach the epub file to the email confirmation as an attachment? Or, include the download link in the email. They don’t want customers to have to create an account, so they’d need to be able to access the download links directly from the email without having to log in.



I’d probably not restrict yourself to a pre-built module if you already have all the information in a custom database. The Digital Product extension is useful for simple use cases, but has some drawbacks.

The biggest drawback right now is that files must be available on a publicly accessible URL to allow them to be downloadable after purchase. It sounds like your client is likely some kind of publisher or distributor, and I don’t think they’d appreciate their entire library being public downloadable if people figure out the URL.

You can, however, take some inspiration from the Digital Product module, such as the custom shipment type (rather than custom product) approach to processing purchases.

If you’re dynamically rendering product views, you’ll probably want a custom add to cart snippet to add them dynamically too. You don’t necessarily need to create products on-demand for that, though that could be useful

Adding information to the emails is pretty straightforward:

  1. Listen to the event
  2. Add placeholder data
  3. Use placeholders in the email templates, e.g. like this code sample.

Adding an attachment directly is a little more work. The easiest way to do that would be to wait for Commerce 1.3, which adds a new event that fires right before an email is sent. That allows you to attach any file to an email. Doing it in 1.2 would require a custom comOrderEmailMessage class with additional logic; that requires a lot of duplication right now and is why we added the new event. Hopefully 1.3 will be in prerelease in about 4-6 weeks – it’s a massive release with tons of new goodies.

Given your no-account requirement, you’d likely save a record of who (email/order id) bought what ebook, assign it some really long secret hash, and create an endpoint on the site, provide it that hash, and use that to offer download options.

Hey Mark,

Thanks for the reply - really helpful and I now have a plan!



1 Like