How It Works
Understanding the execution model and data flow in Function Lab.Architecture Overview
Execution Phases
1. Input Processing
When a customer interacts with their cart or checkout, Shopify calls your Function Lab configuration with cart data: Input Structure:2. Facts Computation
If your rules define facts, they’re evaluated first:- Computed once per rule execution
- Available to all conditions and events
- Cached for performance
- Referenced with
$prefix:$total_items
3. Condition Evaluation
For each rule, conditions are evaluated against the input data and computed facts:- Multiple conditions = implicit AND (all must be true)
- Array properties use ANY semantics (any element can match)
oroperator for alternative conditionsnotoperator for negation- Short-circuit evaluation for performance
true (fire events) or false (skip events)
4. Event Execution
When conditions match, events are executed to generate outputs:- Each event generates one or more discount/operation objects
- Multiple events in a rule = multiple outputs
- Parameterized events expand to multiple outputs
- Events can reference facts for dynamic values
5. Output Assembly
All outputs from matching rules are collected and returned to Shopify: For Discounts:Rule Execution Order
Multiple rules are evaluated in order:- All rules are evaluated independently
- Facts are isolated per rule
- Results from all matching rules are combined
- Use
discountApplicationStrategyto control how discounts stack
Discount Application Strategies
Control how multiple discounts combine:FIRST (Default for most scenarios)
MAXIMUM
ALL
Performance Considerations
Efficient Condition Ordering
Place simple conditions first:Fact Reuse
Calculate once, use everywhere: ❌ Inefficient:Selector Optimization
Use direct targeting when possible: ❌ Slower:Shopify Integration
Function Types
Function Lab supports all Shopify Function types:- Product Discounts: Line item price adjustments
- Order Discounts: Cart-level discounts
- Shipping Discounts: Delivery cost adjustments
- Delivery Customization: Show/hide/reorder shipping methods
- Payment Customization: Control payment method availability
- Cart Transform: Merge/update line items
Metafield Access
Access custom data via metafields:GID Handling
Shopify IDs are Global IDs (GIDs):- Accept numeric IDs:
"variant_ids": [12345678] - Auto-converts to GIDs internally
- Returns proper GID format to Shopify
Error Handling
Invalid Configuration
- Rules with syntax errors are skipped
- Logs show validation errors
- Other rules continue executing
Runtime Errors
- Fact computation errors return
null - Selector errors skip that event
- Functions fail safely (no discount rather than cart breakage)
Testing
Always test in Shopify admin preview mode before activating.Next Steps
- Testing Guide - Debug and validate your rules
- Advanced Features - Complex conditions and dynamic values
- Performance Optimization - Optimize for speed
