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_typeattribute 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
datetype - Use a middleware layer to validate expiration dates before checkout
- Add a
is_expiredflag to gift card records
Gotchas from Real Deployments
- Inventory Sync Latency: Always use a queue system (like Celery) for inventory updates to avoid race conditions
- Expiration Date Formats: Use ISO 8601 format consistently across all systems
- 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.