1

This is more advanced: We want to sell activation keys for digital products. We know that in Craft a variant has a stock. So every single item "in" the stock needs a unique activation key. When we sell one item of the stock we sell a activation key. Of course, an activation key can only be sold once. For each product we will have a limited number of activation keys, so this will be reflected by the stock number itself.

We know that this feature requires some customization. Question is: is this achievable in an easy or hard way via Craft Commerce? How would an elegant approach look like?

3
  • What is your actual question? What you describe sounds actually like the default behavior. You have a stock for each variant and when you sell it, the stock is reduced until it's empty. Until then you can send your unique keys and generate new unique keys that are stored and can be traced back by the order. Is your question how to generate your keys beforehand when you set the amount of items and not to generate them in runtime? – Robin Schambach Apr 4 '19 at 15:24
  • Hi Robin, an example for clarification: Let´s say we got 20 activation keys for an online game we want to sell. So we have a stock of 20 pieces. For every item in the stock we want to associate an activation key (that is persisted in the db). – blacksmith Apr 8 '19 at 8:58
  • We want to make sure that every time we augment the stock we have the same amount of unsold activation keys stored in our database. The background is: we only want to sell activation keys we have in our system. Once an item in the stock is sold the corresponding activation key will be marked as sold. Maybe directly associating every piece in the stock to exactly one activation key is not the best approach. Question is: can it be done in an elegant way? Or is there a far better approach in doing that? – blacksmith Apr 8 '19 at 9:03
1

You can use the beforeGenerateLicenseKey event and query for a custom key in your custom table and set the license key

Event::on(License::class, License::EVENT_GENERATE_LICENSE_KEY, function(GenerateKeyEvent $e) {
    $licenseKey = (new Query())
                      ->select(['mykey'])
                      ->from('{{%mytable}}')
                      ->where(['used' => false])
                      ->scalar();

    $e->licenseKey = $licenseKey;
});

And when you insert new keys in your custom DB table, you can change the stock of your variant accordingly

$variant = //grab the variant you need for your product
$variant->stock = (new Query())->from('mytable')->count();
// store the variant
2
  • Thanks, Robin. Now we also know that a plugin called "Digital Products" is able to handle this ;) Very cool. – blacksmith Apr 8 '19 at 14:04
  • Oh I assumed your "We want to sell activation keys for digital products" was already refering to the plugin. That's why my first comment had so many question marks – Robin Schambach Apr 8 '19 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.