All Projects → craftcms → digital-products

craftcms / digital-products

Licence: MIT license
Sell digital products with Craft Commerce.

Programming Languages

PHP
23972 projects - #3 most used programming language
Twig
543 projects

Projects that are alternatives of or similar to digital-products

smartdown.craft-plugin
Bringing the unbridled joy of Markdown Extra and Smartypants to your Craft websites.
Stars: ✭ 26 (+85.71%)
Mutual labels:  craftcms, craftcms-plugin
commerce-stripe
Stripe payment gateway for Craft Commerce
Stars: ✭ 21 (+50%)
Mutual labels:  craftcms, craft-commerce
craft-entriessubset
Craft field type plugin that extends the core Entries field type to give extra settings
Stars: ✭ 27 (+92.86%)
Mutual labels:  craftcms, craftcms-plugin
formie
The most user-friendly forms plugin for Craft CMS.
Stars: ✭ 73 (+421.43%)
Mutual labels:  craftcms, craftcms-plugin
craft-assetusage
Craft plugin adds a column to see which assets are used or unused.
Stars: ✭ 28 (+100%)
Mutual labels:  craftcms, craftcms-plugin
Craft-UserCreator
Allow you to generate users en masse, simply.
Stars: ✭ 16 (+14.29%)
Mutual labels:  craftcms, craftcms-plugin
Form-Builder
Craft CMS plugin that lets you create and manage forms for your front-end.
Stars: ✭ 16 (+14.29%)
Mutual labels:  craftcms, craftcms-plugin
Stamp-Craft
Plugin for adding timestamp to filenames.
Stars: ✭ 28 (+100%)
Mutual labels:  craftcms, craftcms-plugin
craft3-collections
Clean up those complex templates with Laravel Collections
Stars: ✭ 24 (+71.43%)
Mutual labels:  craftcms, craftcms-plugin
craft-commerce-widgets
Insightful widgets for Craft CMS Commerce stores
Stars: ✭ 33 (+135.71%)
Mutual labels:  craftcms, craftcms-plugin
craft-select2
Filter / search a <select> using the popular Select2 fieldtype for Craft CMS
Stars: ✭ 18 (+28.57%)
Mutual labels:  craftcms, craftcms-plugin
craft-audit
Audit log for Craft 3
Stars: ✭ 18 (+28.57%)
Mutual labels:  craftcms, craftcms-plugin
Craft-Embedly
Embed.ly plugin for Craft CMS
Stars: ✭ 21 (+50%)
Mutual labels:  craftcms, craftcms-plugin
events
Craft CMS Plugin for events management and ticketing.
Stars: ✭ 19 (+35.71%)
Mutual labels:  craftcms, craftcms-plugin
Craft-CacheWarmer
Warm up your cache with a single request.
Stars: ✭ 38 (+171.43%)
Mutual labels:  craftcms, craftcms-plugin
VarnishPurge-Craft
Craft plugin for purging Varnish when elements are saved.
Stars: ✭ 33 (+135.71%)
Mutual labels:  craftcms, craftcms-plugin
molecule
⚛️ Grab Twig components, CSS and JS files outside the primary template folder
Stars: ✭ 20 (+42.86%)
Mutual labels:  craftcms, craftcms-plugin
spoke-and-chain
Craft CMS + Craft Commerce demo site.
Stars: ✭ 24 (+71.43%)
Mutual labels:  craftcms, craft-commerce
Craft-TemplateSelect
Allows you to select templates for an entry in Craft CMS
Stars: ✭ 30 (+114.29%)
Mutual labels:  craftcms, craftcms-plugin
Craft-User-Manual
📚 Help Section Plugin for Craft CMS.
Stars: ✭ 86 (+514.29%)
Mutual labels:  craftcms, craftcms-plugin

Digital Products icon

Digital Products

This plugin makes it possible to sell licenses for digital products with Craft Commerce.

Requirements

Digital Products requires Craft 4.0.0 and Craft Commerce 4.0.0 or later.

Installation

You can install this plugin from the Plugin Store or with Composer.

From the Plugin Store

Go to the Plugin Store in your project’s Control Panel and search for “Digital Products”. Then click on the “Install” button in its modal window.

With Composer

Open your terminal and run the following commands:

# go to the project directory
cd /path/to/my-project.test

# tell Composer to load the plugin
composer require craftcms/digital-products

# tell Craft to install the plugin
./craft install/plugin digital-products

Events

The beforeSaveProductType and afterSaveProductType events

Plugins can be notified immediately before or after a product type is saved so your plugin can take action if needed:

use craft\digitalproducts\events\ProductTypeEvent;
use craft\digitalproducts\services\ProductTypes;
use yii\base\Event;

// ...

Event::on(
    ProductTypes::class, 
    ProductTypes::EVENT_BEFORE_SAVE_PRODUCTTYPE, 
    function(ProductTypeEvent $e) {
        // Custom code to be executed when a product type is saved
    }
);

The beforeGenerateLicenseKey event

Plugins get a chance to provide a license key instead of relying on Digital Products to generate one.

use craft\digitalproducts\elements\License;
use craft\digitalproducts\events\GenerateKeyEvent;
use craft\digitalproducts\Plugin as DigitalProducts;
use yii\base\Event;

// ...

Event::on(
    License::class, 
    License::EVENT_GENERATE_LICENSE_KEY, 
    function(GenerateKeyEvent $e) {
        $licenseService = DigitalProducts::getInstance()->getLicenses();
        
        do {
            $licenseKey = // custom key generation logic...
        } while (!$licenseService->isLicenseKeyUnique($licenseKey));

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

Eager loading

Both licenses and products have several eager-loadable properties.

Licenses

  • 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.

Products

  • existingLicenses eager-loads all the existing licenses for the currently logged in Craft User.

Examples

Displaying the licensed product for the currently logged in Craft User.

{% if currentUser %}
    {% set licenses = craft.digitalProducts
        .licenses
        .owner(currentUser)
        .with(['product', 'order'])
        .all()
    %}

    <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="{{ 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="/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 %}

Checking if currently logged in user is licensed to access a product.

{% set products = craft.digitalProducts
    .products
    .type('onlineCourses')
    .with(['existingLicenses'])
    .all()
%}

{% if products|length %}
    <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.existingLicenses|length %}
                            You already own this product.
                        {% else %}
                            <a href="{{ product.getUrl() }}">Get it now!</a>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endif %}

GraphQL

Digital products may be queried with GraphQL. Please read the getting started docs to get up to speed with how Craft CMS handles GraphQL requests.

The GraphQL implementation provides two query options: digitalProducts for returning multiple products, and digitalProduct for returning a single product.

An example query and response

Query payload

query {
    digitalProducts(type: "eBooks", limit: 2) {
        title,
        sku,
        price
    }
}

The response

{
    "data": {
        "digitalProducts": [
            {
                "title": "Breaking Bad: The Recipes",
                "sku": "BB-TR",
                "price": 14.99
            },
            {
                "title": "The Clone Wars: Color The Clones",
                "sku": "TCW-CTC",
                "price": 7.95
            }
        ]
    }
}

The digitalProducts/digitalProduct query

Both the queries use the same argument set.

Argument Type Description
id [QueryArgument] Narrows the query results based on the elements’ IDs.
uid [String] Narrows the query results based on the elements’ UIDs.
status [String] Narrows the query results based on the elements’ statuses.
unique Boolean Determines whether only elements with unique IDs should be returned by the query.
title [String] Narrows the query results based on the elements’ titles.
sku [String] Narrows the query results based on the digital products’ SKUs.
slug [String] Narrows the query results based on the elements’ slugs.
uri [String] Narrows the query results based on the elements’ URIs.
search String Narrows the query results to only elements that match a search query.
relatedTo [Int] Narrows the query results to elements that relate to any of the provided element IDs. This argument is ignored, if relatedToAll is also used.
relatedToAll [Int] Narrows the query results to elements that relate to all of the provided element IDs. Using this argument will cause relatedTo argument to be ignored.
ref [String] Narrows the query results based on a reference string.
fixedOrder Boolean Causes the query results to be returned in the order specified by the id argument.
inReverse Boolean Causes the query results to be returned in reverse order.
dateCreated [String] Narrows the query results based on the elements’ creation dates.
dateUpdated [String] Narrows the query results based on the elements’ last-updated dates.
offset Int Sets the offset for paginated results.
limit Int Sets the limit for paginated results.
orderBy String Sets the field the returned elements should be ordered by
type [String] Narrows the query results based on the digital products’ prdocut type handles.
typeId [QueryArgument] Narrows the query results based on the digital products’ product types, per the types’ IDs.
postDate [String] Narrows the query results based on the digital products’ post dates.
before String Narrows the query results to only digital products that were posted before a certain date.
after String Narrows the query results to only digital products that were posted on or after a certain date.
expiryDate [String] Narrows the query results based on the digital products’ expiry dates.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].