Commerce Stock

Provides a comprehensive stock management framework for Drupal Commerce, enabling inventory tracking, stock transactions, and availability checking for purchasable entities.

commerce_stock
6,650 sites
68
drupal.org

Install

Drupal 11, 10 v3.0.0
composer require 'drupal/commerce_stock:^3.0'
Drupal 9 v8.x-1.3
composer require 'drupal/commerce_stock:8.x-1.3'
Drupal 8 v8.x-1.2
composer require 'drupal/commerce_stock:8.x-1.2'

Overview

Commerce Stock is a complete inventory management solution for Drupal Commerce stores. It provides a transaction-based stock management system that tracks stock levels through discrete transactions (receives, sales, returns, movements) rather than simple absolute values.

The module uses a pluggable architecture allowing different stock storage backends, with a built-in local database storage implementation. It integrates with Commerce order workflows to automatically create stock transactions when orders are placed, completed, cancelled, or modified.

The system supports multiple stock locations, making it suitable for businesses with multiple warehouses or fulfillment centers. Stock enforcement can prevent customers from ordering more items than available, with configurable error messages and validation at cart and checkout stages.

Features

  • Transaction-based inventory management with full audit trail of all stock movements
  • Automatic stock deduction when orders are placed or completed, with configurable event triggers
  • Support for multiple stock locations and warehouses per store
  • Stock enforcement to prevent overselling, with validation on add-to-cart, cart page, and checkout
  • Stock Level field type for product variations with multiple widget options (absolute, transaction-based, link to form)
  • Pluggable stock service architecture allowing custom stock storage backends
  • Configurable stock events including order place, cancel, update, and item-level events
  • Stock transactions API for programmatic stock management (receive, sell, return, move)
  • Real-time or cron-based stock level aggregation options
  • Multi-store support with per-store primary location and available fulfillment locations

Use Cases

Basic E-commerce Store with Stock Tracking

For a simple store selling physical products, enable all Commerce Stock submodules. Add a Stock Level field to your product variation types, set the default service to Local Stock, and create a single stock location representing your warehouse. Stock will automatically decrease when orders are placed and can be manually increased through the transaction form when receiving new inventory.

Multi-warehouse Fulfillment

For businesses with multiple warehouses, create a Stock Location entity for each warehouse. Add 'Available stock locations' and 'Stock allocation location' entity reference fields to your Store entity type, allowing each store to define which warehouses can fulfill orders and which is the primary location for transactions. Stock checks will aggregate across all available locations.

Preventing Overselling

Enable the Commerce Stock Enforcement submodule to prevent customers from purchasing more items than available. The module validates stock at add-to-cart, cart updates, and checkout. Customize the error messages in the enforcement settings to match your brand voice.

Dropshipping or Made-to-Order Products

For products that don't need stock tracking (dropshipped items, digital products, made-to-order items), leave the stock service as 'Always in stock' for those variation types. This allows mixing tracked and untracked products in the same store.

Programmatic Stock Management

Use the StockServiceManager service to create transactions programmatically. Call $stockServiceManager->receiveStock() for incoming inventory, sellStock() for manual sales, returnStock() for returns, and moveStock() for warehouse transfers. Each method creates proper transaction records maintaining the audit trail.

Tips

  • Use the 'Simple stock transaction' widget instead of 'Absolute stock level' for production sites to maintain proper transaction history and avoid race conditions
  • Enable cron aggregation mode for high-volume stores to improve performance by batching stock level calculations
  • Always create stock transactions through the API or forms rather than directly modifying database tables to maintain data integrity
  • Consider using the 'Real-time' aggregation mode for low-volume stores where immediate stock accuracy is critical
  • Add the commerce_stock_always_in_stock boolean field to product variations to mark specific items that should never be stock-checked

Technical Details

Admin Pages 7
Stock /admin/commerce/config/stock

Main stock configuration landing page providing access to all stock-related settings and management pages.

Stock configuration /admin/commerce/config/stock/settings

Configure the default stock service and per-entity-type stock service assignments. Select which stock events plugin handles order lifecycle events.

Local Stock Configuration /admin/commerce/config/stock/local_stock_config

Configure how the local stock service handles transactions and cron-based stock level updates.

Stock Enforcement Settings /admin/commerce/config/stock/enforcement/settings

Configure the error messages displayed when customers attempt to purchase more items than available in stock.

Stock locations /admin/commerce/commerce_stock_location

Manage stock locations representing warehouses, stores, or fulfillment centers where inventory is stored.

Stock transactions /admin/commerce/config/stock/transactions1

Create stock transactions for product variations. First step allows selecting the product variation to transact.

Create stock transaction /admin/commerce/config/stock/transactions2

Second step of transaction creation with full transaction details including type, location, quantity, and notes.

Permissions 4
Access Commerce Stock administration pages

Use Commerce Stock administrative pages and functionality.

Administer Commerce Stock

Administer the Commerce Stock settings. Restricted access permission.

Administer stock location types

Maintain the types of stock location available and the fields that are associated with those types. Restricted access permission.

Use Commerce Stock transaction form

Create a Commerce Stock transaction through the Commerce Stock UI transaction form.

Hooks 2
hook_commerce_stock_service_alter

Allows modules to alter the stock service selected for a purchasable entity.

hook_form_alter

Stock Enforcement uses hook_form_alter to add validation to add-to-cart, cart, and checkout forms.

Troubleshooting 5
Stock levels not updating after order placement

Check Stock configuration to ensure the default service is set to 'Local stock' for your product variation type. Verify the 'Order complete transaction' setting matches your order workflow. For orders using custom workflows, ensure the transition names match expected values.

'The StockServiceManager didn't return a location' error

Create at least one active Stock Location entity. If using multi-store, ensure the store has the stock allocation location field configured with a valid location.

Stock shows as 0 for all products

After adding the Stock Level field, you need to create initial stock transactions using the Stock transactions form or programmatically. The field doesn't have default stock values.

Cart allows adding more items than in stock

Enable the Commerce Stock Enforcement module. By default, the base module tracks stock but doesn't prevent overselling.

Stock field widget shows 'no valid context' message

This occurs when the system cannot determine the store context. Ensure the product is assigned to at least one store and that a current store can be resolved.

Security Notes 3
  • The 'Administer Commerce Stock' permission grants access to change stock services and event handling - restrict to trusted administrators
  • Stock transaction form access is controlled by 'Use Commerce Stock transaction form' permission - assign carefully as it allows manual stock adjustments
  • Stock location type administration is a restricted permission as it affects the data model