<p>You can use the <a href="https://github.com/craftcms/digital-products#the-beforegeneratelicensekey-event" rel="nofollow noreferrer">beforeGenerateLicenseKey</a> event and query for a custom key in your custom table and set the license key</p>

<pre class="lang-php prettyprint-override"><code>Event::on(License::class, License::EVENT_GENERATE_LICENSE_KEY, function(GenerateKeyEvent $e) {
    $licenseKey = (new Query())
                      -&gt;select(['mykey'])
                      -&gt;from('{{%mytable}}')
                      -&gt;where(['used' =&gt; false])
                      -&gt;scalar();

    $e-&gt;licenseKey = $licenseKey;
});
</code></pre>

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

<pre><code>$variant = //grab the variant you need for your product
$variant-&gt;stock = (new Query())-&gt;from('mytable')-&gt;count();
// store the variant
</code></pre>

CC BY-SA 4.0
2019-04-08T14:04:18.213Z
Thanks, Robin. Now we also know that a plugin called "Digital Products" is able to handle this ;) Very cool.
blacksmith
CC BY-SA 4.0
2019-04-08T14:08:02.240Z
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
CC BY-SA 4.0
2019-04-08T10:05:13.420Z
2019-04-08T10:05:13.420Z
Robin Schambach
<p>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. </p>

<p>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?</p>

CC BY-SA 4.0
2019-04-04T15:24:52.823Z
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
CC BY-SA 4.0
2019-04-08T08:58:51.837Z
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
CC BY-SA 4.0
2019-04-08T09:03:07.560Z
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
<plugin-craftcommerce><stock>
Individual persistent property per item in stock
# Individual persistent property per item in stock

(Asked by: username_0 on 2019-04-04)

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?

**Tags:** plugin-craftcommerce, stock
> **Comments:**
> **username_1**: 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?  
> **username_0**: 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).  
> **username_0**: 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?  

---

## 1 answers

### Answer by username_1 on 2019-04-08. Score: 1 | Accepted answer

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

> **Comments:**
> **username_0**: Thanks, Robin. Now we also know that a plugin called "Digital Products" is able to handle this ;) Very cool.  
> **username_1**: 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  

---

