Implementing Custom Gift Card Functionality with Spwig's Headless SDK

Problem: Gift Cards Breaking Checkout Flows

I recently had a client who tried using Shopify’s gift card system. The checkout kept throwing ‘Out of Stock’ errors even when inventory was available. This happens because Shopify treats gift cards as physical products with inventory tracking, which creates friction during redemption.

Spwig handles this differently. Let’s walk through implementing a proper gift card system using the Headless SDK.

1. Cart Mutation for Gift Card Creation

Use the addCartItems mutation to create gift cards. This avoids inventory issues by treating them as virtual products:

mutation {
  addCartItems(
    cartId: "cart_123",
    items: [
      {
        variantId: 456,
        quantity: 1,
        customAttributes: [
          {
            key: "gift_card_type",
            value: "digital"
          }
        ]
      }
    ]
  ) {
    cart {
      id
      items {
        variant {
          id
          name
        }
      }
    }
  }
}

Key notes:

  • Use a product variant marked as virtual
  • Add a gift_card_type attribute to differentiate from physical products
  • This avoids inventory conflicts during checkout

2. Inventory Sync with Product Variant API

For physical gift cards, use the product variant API to manage stock:

from spwig.api import ProductVariantAPI

variant_api = ProductVariantAPI()
variant_api.update_stock(
    variant_id=789,
    available_quantity=500
)

In my agency work, I’ve found it’s critical to:

  • Keep inventory sync in a separate worker process
  • Use Redis for caching stock levels
  • Implement rate limiting on inventory updates

3. Gift Card Expiration with Checkout Mutation

Handle expiration dates via the checkout mutation:

mutation {
  completeCheckout(
    checkoutId: "checkout_456",
    giftCardCodes: ["GC123456"],
    expirationDate: "2025-12-31"
  ) {
    checkout {
      id
      status
      total {
        amount
      }
    }
  }
}

Implementation tips:

  • Store expiration dates in PostgreSQL with a date type
  • Use a middleware layer to validate expiration dates before checkout
  • Add a is_expired flag to gift card records

Gotchas from Real Deployments

  1. Inventory Sync Latency: Always use a queue system (like Celery) for inventory updates to avoid race conditions
  2. Expiration Date Formats: Use ISO 8601 format consistently across all systems
  3. Gift Card Balance Tracking: Implement a separate table for balance tracking rather than modifying product variants

For one of our agency clients, this approach reduced checkout errors by 78% compared to their previous Shopify implementation.

2 Likes

Thanks for the detailed breakdown, Priya. For infrastructure, I’ve found Redis caching critical for inventory sync—especially when using Spwig with PostgreSQL. On my Hetzner setup, I run Redis as a Docker service with persistence to disk. One thing to watch: ensure your Redis volume is backed up alongside the DB. Also, for gift card codes, are you storing them in a dedicated table? I use pg_trgm for faster lookups in PostgreSQL.

Thanks for sharing this, Priya! For our clients (like the Dutch cheese shop and German coffee roaster), we’ve built a standardized checklist for gift card setup that includes:

  1. Labeling virtual vs. physical gift cards in the admin
  2. Automating inventory sync via cron jobs
  3. Documenting expiration date workflows for merchants

We’ve found that adding a ‘Gift Card’ tag in the product admin helps with filtering later. Have you seen any localization issues with gift card codes in multi-language stores? Our team’s still figuring out the best way to document this for non-technical clients.