Configuring Drupal Commerce Module for an Online Store
Drupal Commerce is not a plugin, but a set of modules that embed e-commerce logic directly into Drupal's architecture. A product is a node with fields, a variant is an entity, an order is an entity with its own workflow. This flexibility allows building non-standard catalogs and processes, but requires understanding Drupal's Entity API and Commerce 2.x specifics.
Commerce 2.x vs Commerce 1.x
Commerce 2.x is rewritten for Drupal 8+ and is incompatible with the first version. Key differences:
| Commerce 1.x (D7) | Commerce 2.x (D8/9/10) | |
|---|---|---|
| Architecture | Rules + Features | Plugins + YAML config |
| State management | Rules | State Machine |
| Multisite | Difficult | Stores out of the box |
| API | Hook-based | OOP + Events |
If the project is on Drupal 7 — consider either Commerce 1.x (legacy) or migration to D10.
Basic installation via Composer
composer require drupal/commerce drupal/commerce_price drupal/commerce_store \
drupal/commerce_product drupal/commerce_order drupal/commerce_cart \
drupal/commerce_checkout drupal/commerce_payment drupal/commerce_tax
drush en commerce commerce_product commerce_cart commerce_checkout \
commerce_payment commerce_tax commerce_order -y
drush cr
Minimum dependencies are installed automatically via Composer.
Store configuration
Commerce 2.x supports multiple stores in a single Drupal instance:
/admin/commerce/config/stores → Add store
Store configuration:
- Default currency: BYN / RUB / USD (must match site base currency)
- Countries: list of countries for shipping and billing addresses
- Default tax type: if the store pays VAT, the tax type is specified
- Billing countries: countries for which tax will be calculated
A store is tied to a site URL — important for multisite setup.
Product types
Commerce uses the product/variation concept: Product is the "product card", Variation is the specific SKU with price and attributes.
Creating a product type:
/admin/commerce/config/product-types → Add product type
Configuring variations:
/admin/commerce/config/product-variation-types → Add variation type
→ Add attributes: Color, Size
→ Auto-generate variations: Yes
After creating attributes, Drupal Commerce automatically generates a variation matrix on the product edit form.
Programmatic product management:
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
// Creating a variation
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'CHAIR-BLK-L',
'price' => new Price('1499.00', 'RUB'),
'field_color' => ['target_id' => $colorTermId],
'field_size' => 'L',
'status' => TRUE,
]);
$variation->save();
// Creating a product with variation
$product = Product::create([
'type' => 'default',
'title' => 'Office chair',
'stores' => [$storeId],
'variations' => [$variation],
'status' => TRUE,
]);
$product->save();
Checkout Flow
Commerce 2.x allows complete customization of the checkout process via Checkout Flows configuration:
/admin/commerce/config/checkout-flows → Manage
Available steps (panes):
-
commerce_checkout_panes.login— authentication/guest -
commerce_checkout_panes.order_information— shipping address -
commerce_checkout_panes.payment_information— payment details -
commerce_checkout_panes.review— confirmation -
commerce_checkout_panes.completion— success page
Custom pane is created as a plugin:
/**
* @CommerceCheckoutPane(
* id = "custom_gift_note",
* label = @Translation("Gift note"),
* default_step = "order_information",
* )
*/
class GiftNotePane extends CheckoutPaneBase
{
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form): array
{
$pane_form['gift_note'] = [
'#type' => 'textarea',
'#title' => $this->t('Message to recipient'),
'#rows' => 3,
];
return $pane_form;
}
}
Connecting payment gateways
Commerce 2.x has official and community modules for payment gateways:
# Stripe
composer require drupal/commerce_stripe
drush en commerce_stripe -y
# PayPal
composer require drupal/commerce_paypal
# For RF/CIS — YuKassa (unofficial)
composer require drupal/commerce_robokassa
Gateway configuration:
/admin/commerce/config/payment-gateways → Add payment gateway
→ Plugin: Stripe
→ Mode: Live / Test
→ Secret key: sk_live_...
→ Publishable key: pk_live_...
Inventory management
Commerce 2.x does not include inventory out of the box. Module is used:
composer require drupal/commerce_stock
drush en commerce_stock commerce_stock_local -y
After that, a stock field appears on product variations. The module supports multiple locations (warehouses):
/admin/commerce/config/stock → Stock service: Local stock
/admin/commerce/config/stock-locations → Add location
Programmatic stock management:
use Drupal\commerce_stock\StockServiceManagerInterface;
$stockService = \Drupal::service('commerce_stock.service_manager')
->getService($productVariation);
// Get stock level
$level = $stockService->getStockLevel($productVariation, $locations);
// Change stock (receipt)
$stockService->createTransaction(
$productVariation,
$locationId,
[],
100, // quantity
new Price('0', 'RUB'),
'RUB',
TRANSACTION_TYPE_IN,
[]
);







