Behavior
Deep dive into how the checkout gift extension evaluates rules, manages the cart queue, and handles different product scenarios.
Rule Evaluation Pipeline
When the extension activates, it follows this pipeline for each automation:
Input Object
The rule evaluation uses a comprehensive input object built from the checkout state:
Processing Matched Rules
When a rule evaluates to true, the extension determines the action based on the rule's configuration:
Auto-Remove Mode
If a rule is configured for checkout removal, the extension removes the specified products from the cart:
Modal Selector (Customer Picks)
When customers need to choose from available products:
Product picker:
- Calculates remaining quantity (total allowed minus already in cart)
- Shows a modal with product cards and variant selection
- Supports the "decline" option via cart attributes
Collection picker:
- Shows products from the specified collection(s)
- Tracks added quantity across all collection products
- Excludes already-added products
Automatic (No Interaction)
Products are added to the cart automatically without customer input:
Free gift (100% discount):
- Added immediately to the cart queue
- Includes internal attributes for discount processing
Non-free products:
- Added to the confirmation queue
- Shown in a confirmation overlay before adding
Discount Handling
Automatic Discounts
When a rule has an automatic discount:
- The discount data is attached as a line attribute on the cart item
- The Discount Function reads this attribute server-side and applies the discount
Code Discounts
When a rule has a code discount:
- The extension checks if the code is already applied
- If not, queues the discount code application automatically
Auto-Clean Logic
The auto-clean system removes products from the cart when their associated rules are no longer valid:
Additionally, when automations are loaded:
- Products whose variant IDs don't match the current rule's entries are removed
- Collection-type rules are handled separately in the modal
Cart Mutation Queue
All cart mutations are processed sequentially through a queue system to prevent race conditions:
The queue is critical for stability. Cart mutations must be sequential because Shopify's checkout API doesn't handle concurrent mutations well. Processing multiple mutations in parallel can cause race conditions and lost updates.
doNotEvaluateStorefrontRule Setting
This setting deserves special attention because it solves a common problem:
Problem: Some rules use conditions that only exist on the storefront (e.g., "customer is on the product page"). At checkout, these conditions can't be evaluated, so the rule fails — causing the product to be removed.
Solution: When doNotEvaluateStorefrontRule is enabled:
- The extension identifies storefront-only rules
- Their titles are tracked internally
- During auto-clean and rule evaluation, products with matching titles are skipped entirely
- This preserves the storefront's decision without re-evaluating conditions that don't apply at checkout
Country/Region Restrictions
The extension tracks excluded variants that fail due to country/region restrictions:
- When a product add fails (variant unavailable in the current shipping country), the variant ID is stored in the excluded list
- On subsequent evaluations, excluded variants are skipped
- This prevents infinite retry loops for region-restricted products
Unavailable Product Banner
When a product add fails (e.g., sold out, region-restricted), the extension displays a dismissible banner informing the customer that the item is unavailable.