E-commerce in Drupal 8 is a topic of interest for many developers. If you are among them, you’ve come to the right place — our Drupal developers have written a blog post on the Commerce 2.x module. After our developers’ posts on theming, pop-up creating, module building in Drupal 8 and more, this one rounds out the picture. So let’s give her the floor.
Drupal Commerce is a popular module for ecommerce development solutions of varying complexity. This module is "out of the box" ready to help you set up an online store with basic functionality. This article provides an overview of the Commerce 2.x module for Drupal 8.
Installing Drupal Commerce 2.x
Before installing Drupal Commerce, make sure that your server has a Composer dependency manager installed. In Drupal 8, Commerce requires the installation of libraries, which are now installed not manually but with the help of Composer.
You can install Drupal, as well as Commerce together with the dependent modules and libraries with this command:
composer create-project drupalcommerce/project-base mystore --stability dev
Everything will be installed in the mystore folder.
Next, you need to open the site in your browser and complete the installation process.
Now, Drupal Commerce and its dependent modules and libraries will be enabled.
If you look at the structure of folders, you will see that Drupal core and modules are in the mystore/web folder, Drupal Console is in mystore/vendor/bin, and Drush is in mystore/drush.
An example of the command for installing new modules:
composer require "drupal/devel:8.1.x-dev"
Updating existing modules:
composer update drupal/address
If you need to install Drupal Commerce to an existing site, use the following command (at the root of the site):
composer config repositories.drupal composer https://packagist.drupal-composer.org
This command will find Drupal Commerce and other modules and libraries.
The installation is carried out with this command:
composer require "drupal/commerce 8.2.x-dev"
Then you need to install Drupal Console, if it is not yet installed. Drupal Console is a new command line interface for Drupal, which allows you to generate code templates, do the debugging and so on.
Installing Drupal Console:
curl https://drupalconsole.com/installer -L -o drupal.phar
The installation command without using Curl:
php -r "readfile('https://drupalconsole.com/installer');" > drupal.phar mv drupal.phar /usr/local/bin/drupal chmod +x /usr/local/bin/drupal drupal init --override drupal check
Now, let’s enable the modules using Drupal Console.
drupal module:install commerce_product commerce_checkout commerce_cart commerce_tax
After the successful installation you will see this message:
[OK] The following module(s) were installed successfully: commerce_product, commerce_checkout, commerce_cart, commerce_tax, commerce, address, entity, inline_entity_form, commerce_price, commerce_store, commerce_order, profile, state_machine
Settings in the administrative interface
Go to Commerce settings page (admin/commerce/config).
Firstly, you need to select your shop currency (admin/commerce/config/currency). By default, the currency list is empty (you will see the message “there is no currency yet”). The list of currencies needs to be imported. Select the desired ones on the currency import page (admin/commerce/config/currency/import).
The currencies are stored in the library, which has already been installed together with the modules.
Now, at the Currency page, we see a list of currencies.
2. Online store
Now, let’s proceed to creating the store.
In Commerce 1.x (Drupal 7) there was no such entity as "shop", and in the version for Drupal 8, you can create multiple stores, which may vary by currencies, tax settings, shipping countries and so on.
By default, there is one type of store — default. But you can create your own types of stores (on the admin/commerce/config/store-types page) and add a variety of fields. On the admin/commerce/stores page, click Add new store.
At this step, we configure the store: give it a name, specify its email, country, currency, and other settings.
Here we should note the main difference of Drupal Commerce 2.x (for Drupal 8) from Drupal Commerce 1.x (for Drupal 7). In Commerce 2.x, we now have such entities as Product and ProductVariation. Product replaces product display of Commerce 1.x (node type, which has been linked to the products). ProductVariation is an analogue of Commerce Product.
Thus, like in Drupal 7, we can create a lot of different ProductVariation entities for the same Product entity. For example, a "dress" is a product which has many product variations (eg, different sizes and colors). The size and color are the product’s attributes. A set of product attributes defines the product variation.
3.1 Product Attributes
First, we create product attributes.
On the Product Attributes page (admin/commerce/product-attributes) we add our attribute (eg, product size) and select a value that it can have.
For example, let’s add "size".
3.2 Product variations
Next, on the Product variation types page (admin/commerce/config/product-variation-types) we choose the Default product variation (or create our own), and add this attribute to the variation.
A product variation can have several attributes.
All the attributes of the product variations can be found in the list of fields (admin/commerce/config/product-variation-types/default/edit/fields)
In our example, the Default variation has one attribute — "size". This is a field that can be changed just like other fields. By clicking the Edit button, we can go to the editing mode.
As an example, let’s set a default value.
When creating a new type of product variations, you can use the already existing attributes.
A product variation may have various fields of various types (text, numeric, etc.), as well as other entities. You can add a new field on this page: admin/commerce/config/product-variation-types/VARIANT_NAME/edit/fields/add-field
To create a product, let’s go to the admin/commerce/products page.
Creating a new product is similar to creating a node. You need to specify the name of the product, if necessary, its description (a product, like any other entity, may have other fields as well), and then create product variations. When creating the variations, you need to specify their SKU, price, and fill in the fields (eg, the "size" field that we created earlier).
As a result, we will have a product named “Dress” and the variations of this product in various sizes.
You can create the types of products and create fields for them on the admin/commerce/config/product-types page
This is how our first product looks.
You can customize the order of the fields and their display on the admin/commerce/config/product-types/TYPE_NAME/edit/form-display page.
We have now done basic settings for an online store. The Drupal Commerce 2.x module supports a variety of options for implementing the desired features. This, for example, includes taxes (the Tax submodule) and the ability to customize orders and order processing workflow (the Workflow submodule). Also, at the moment of writing, people are working to add discount functionality.
Find more information about Commerce 2.x on the following links: