Commerce Gift Card

Provides complete gift card functionality for Drupal Commerce, including gift card creation, sale, redemption, balance management, and transaction tracking.

commerce_giftcard
1,228 sites
25
drupal.org

Overview

Commerce Gift Card is a comprehensive solution for implementing gift card functionality in Drupal Commerce stores. It introduces two new entity types: Gift Cards (content entities that store unique codes and balances) and Gift Card Types (configuration entities that define code generation patterns and display settings).

The module supports the full gift card lifecycle: merchants can manually create individual gift cards, bulk generate cards with configurable code lengths, or set up products that automatically generate gift cards upon purchase. Customers can redeem gift cards during checkout through an integrated redemption pane, with the balance automatically applied as an order adjustment.

Every balance change is recorded as a transaction, providing a complete audit trail of gift card usage. The module also includes refund functionality, allowing administrators to return amounts to gift cards from completed orders. Store-specific restrictions can limit where gift cards are valid, and currency validation ensures gift cards match order currencies.

Features

  • Create and manage gift cards with unique alphanumeric codes and monetary balances
  • Define multiple gift card types with configurable code generation patterns (customizable length)
  • Bulk generate gift cards in batches with specified type, balance, and store restrictions
  • Sell gift cards as products by adding the purchasable trait to product variation types
  • Redeem gift cards during checkout via a dedicated checkout pane with configurable multi-card support
  • Automatic balance deduction when orders are placed with transaction logging
  • Complete transaction history tracking all balance changes with references to related entities
  • Refund gift card amounts from completed orders back to the original gift cards
  • Store-specific restrictions to limit gift card validity to selected stores
  • Token-based display labels for customizing how gift cards appear as order adjustments
  • Views integration with pre-configured admin views for managing gift cards and transactions
  • Extensible event system for customizing gift card amount calculations and CRUD operations

Use Cases

Selling gift cards as products

Create a product variation type with the 'Allows to buy gift cards' trait. Create products with this variation type and configure the gift card type and optionally a fixed gift card amount (or leave empty to use the product price). When customers purchase these products, gift cards are automatically generated and assigned to them, with the codes available in the checkout completion message.

Promotional gift cards

Use the bulk generation feature to create gift cards for marketing campaigns. Navigate to /admin/commerce/giftcards/generate, select the type, set the promotional balance, and specify quantity. Distribute the generated codes through your marketing channels.

Customer loyalty rewards

Programmatically create gift cards as rewards by using the entity storage. Create a commerce_giftcard entity with a specific balance and assign the customer as the owner. They can then redeem it on future purchases.

Partial payments with gift cards

Customers can apply gift cards during checkout even if the gift card balance is less than the order total. The full gift card balance is applied, and the customer pays the remainder with another payment method.

Multi-store gift card restrictions

For multi-store setups, gift cards can be restricted to specific stores. When creating or generating gift cards, select the allowed stores. Gift cards will only be redeemable at those stores.

Gift card refunds

When orders with gift card payments are canceled or partially refunded, use the 'Refund gift card' tab on the order to return the amount to the gift card balance. This creates a positive transaction and updates the order adjustments.

Tips

  • Use tokens in the display label (e.g., 'Gift Card [commerce_giftcard:code:value]') to show the specific gift card code in order summaries
  • The order processor runs at priority -1000, meaning gift cards are applied after all other adjustments including promotions and taxes
  • Gift cards with no owner (uid is null) can be redeemed by anyone, while owned gift cards can be viewed by their owners with the 'view own giftcards' permission
  • Transaction comments support translation variables, so messages like 'Used on order with id @order_id' are properly translatable
  • The bulk generator creates 2x the requested quantity of codes, then filters for uniqueness, ensuring high success rates even with shorter code lengths

Technical Details

Admin Pages 8
Gift Cards /admin/commerce/giftcards

Main administration page for viewing and managing all gift cards. Displays a table with gift card code, type, status (enabled/disabled), current balance, owner, transaction count, and creation/update dates. Provides operations for editing, deleting, and adding transactions to each gift card.

Add gift card /admin/commerce/giftcards/add/{commerce_giftcard_type}

Form for creating a new gift card of a specific type. Allows setting the unique code, initial balance, owner, status, and store restrictions.

Generate gift cards /admin/commerce/giftcards/generate

Bulk generation form for creating multiple gift cards at once. Uses batch processing to generate cards in groups of 25.

Transactions /admin/commerce/giftcards/{commerce_giftcard}/transactions

Displays all transactions for a specific gift card, showing the complete history of balance changes.

Add transaction /admin/commerce/giftcards/add-transaction

Form for manually creating a gift card transaction. Can add or subtract from the gift card balance.

Gift Card types /admin/commerce/config/giftcard_types

Configuration page for managing gift card type bundles. Each type defines settings for code generation and display labels.

Add gift card type /admin/commerce/config/giftcard_types/add

Form for creating a new gift card type with code generation settings and display configuration.

Refund gift card /admin/commerce/orders/{commerce_order}/giftcard-refund

Form for refunding gift card amounts back to gift cards used on a completed order. Only available for non-draft orders that have gift card adjustments.

Permissions 6
Administer Commerce gift card types

Allows creating, editing, and deleting gift card type configuration entities.

Administer Commerce gift cards

Full administrative access to all gift cards including viewing, editing, deleting, generating, and managing transactions.

Access gift card overview

Allows viewing the gift card administration list without full administrative permissions.

View own gift cards

Allows users to view gift cards that they own (where they are set as the owner).

Create gift card

Allows creating new gift cards without full administrative permissions.

Create gift card transactions

Allows adding manual transactions to gift cards without full administrative permissions.

Hooks 3
hook_entity_base_field_info

Adds the commerce_giftcards field to commerce_order entities for storing applied gift card references.

hook_preprocess_commerce_checkout_completion_message

Provides gift card codes purchased in an order to the checkout completion message template.

hook_theme

Defines the commerce_giftcard_redemption_form theme hook for the redemption inline form.

Troubleshooting 5
Gift card code is rejected as already in use

Gift card codes must be unique. The validation is case-insensitive, so 'ABC123' and 'abc123' are considered the same. Check existing gift cards or use the automatic code generator.

Gift card cannot be redeemed - currency mismatch

Gift cards can only be used on orders with matching currencies. Ensure the gift card balance currency matches the store's default currency.

Gift card cannot be redeemed - store restriction

If a gift card has store restrictions configured, it can only be redeemed at those specific stores. Check the gift card's store settings.

Transaction fails with negative balance error

Transactions that would result in a negative gift card balance are rejected. This prevents over-redemption. The validation ensures negative transaction amounts don't exceed the current balance.

Purchased gift cards not appearing after checkout

Ensure the product variation type has the 'Allows to buy gift cards' trait and the commerce_giftcard_type field is set on the variation. Gift cards are created during the order placement transition.

Security Notes 5
  • Gift card codes should be treated as sensitive data similar to passwords or API keys
  • The 'administer commerce_giftcard' permission grants full access including bulk generation and viewing all balances
  • Store gift card codes securely and transmit them over encrypted connections
  • Consider implementing rate limiting on the redemption form to prevent brute-force code guessing
  • The code generator uses Drupal's password generator service for cryptographically secure random strings