This plugin makes it possible to sell licenses for digital products with Craft Commerce.
Digital Products requires Craft CMS 2.6 or later and Craft Commerce 1.1 or later.
To install Digital Products, copy the digitalproducts/ folder into craft/plugins/, and then go to Settings → Plugins and click the “Install” button next to “Digital Products”.
Digital Products gets its own configuration file, located at craft/config/digitalproducts.php
. It can have the following config settings:
true
.)false
.)24
.)false
.)Digital Products offers a few hooks that enable other plugins to modify its behavior:
digitalProducts_modifyLicenseKey – Gives plugins a chance to modify a license key when it’s getting generated.
digitalProducts_modifyProductSources – Gives plugins a chance to modify the sources on digital product indexes.
digitalProducts_defineAdditionalProductTableAttributes – Gives plugins a chance to add additional available table attributes to digital product indexes.
digitalProducts_getProductTableAttributeHtml – Gives plugins a chance to override the HTML of the table cells on digital product indexes.
digitalProducts_modifyProductSortableAttributes – Gives plugins a chance to modify the array of sortable attributes on digital product indexes.
digitalProducts_modifyLicenseSources – Gives plugins a chance to modify the sources on license indexes.
digitalProducts_defineAdditionalLicenseTableAttributes – Gives plugins a chance to add additional available table attributes to license indexes.
digitalProducts_getLicenseTableAttributeHtml – Gives plugins a chance to override the HTML of the table cells on license indexes.
digitalProducts_modifyLicenseSortableAttributes – Gives plugins a chance to modify the array of sortable attributes on license indexes.
Digital Products offers a few events that other plugins can listen to:
product
(the digital product) and isNewProduct
(whether it’s new). Event handlers can prevent the digital product from being saved by setting $event->performAction = false
.product
(the digital product).product
(the digital product). Event handlers can prevent the digital product from being saved by setting $event->performAction = false
.product
(the digital product).license
(the license) and isNewLicense
(whether it’s new). Event handlers can prevent the license from being saved by setting $event->performAction = false
.license
(the license).license
(the license). Event handlers can prevent the license from being saved by setting $event->performAction = false
.license
(the license).Both licenses and products have several eager-loadable properties
product
- Allows you to eager-load the product associated with the license.order
- Allows you to eager-load the order associated with the license, if any.owner
- Allows you to eager-load the Craft user that owns the license, if any.isLicensed
- Eager-loads whether the product is licensed for the currently logged in Craft User. {% if currentUser %}
{% set licenses = craft.digitalProducts.licenses.owner(currentUser).with(['products', 'order']) %}
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">Licenses</h3></div>
{% if licenses %}
<table class="table">
<thead>
<tr>
<th>Licensed product</th>
<th>License date</th>
<th>Order</th>
</tr>
</thead>
<tbody>
{% for license in licenses %}
<tr>
<td><a href="https://github.com/pixelandtonic/DigitalProducts/blob/master/{{ license.product.getUrl() }}">{{ license.product.title }}</a></td>
<td>{{ license.dateCreated|date('Y-m-d H:i:s') }}</td>
<td>
{% if license.orderId %}
<a href="https://github.com/pixelandtonic/DigitalProducts/blob/master/store/order?number={{ license.order.number }}">Order no. {{ license.orderId }}</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% else %}
<p>Please log in first</p>
{% endif %}
{% set products = craft.digitalProducts.products({type: 'onlineCourses'}).with(['isLicensed']) %}
{% if products %}
<table class="table">
<thead>
<tr>
<th>Product</th>
<th>License status</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.title }}</td>
<td>
{% if product.isLicensed() %}
You already own this product.
{% else %}
<a href="https://github.com/pixelandtonic/DigitalProducts/blob/master/{{ product.getUrl() }}">Get it now!</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}