Skip to main content

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:
This input is parsed and made available for querying via conditions and facts.

2. Facts Computation

If your rules define facts, they’re evaluated first:
Facts are:
  • 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:
Evaluation Logic:
  • Multiple conditions = implicit AND (all must be true)
  • Array properties use ANY semantics (any element can match)
  • or operator for alternative conditions
  • not operator for negation
  • Short-circuit evaluation for performance
Result: true (fire events) or false (skip events)

4. Event Execution

When conditions match, events are executed to generate outputs:
Event Processing:
  • 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:
For Customizations:

Rule Execution Order

Multiple rules are evaluated in order:
Important:
  • All rules are evaluated independently
  • Facts are isolated per rule
  • Results from all matching rules are combined
  • Use discountApplicationStrategy to control how discounts stack

Discount Application Strategies

Control how multiple discounts combine:

FIRST (Default for most scenarios)

Only the first matching discount is applied. Use when discounts shouldn’t stack.

MAXIMUM

Apply the largest discount found. Good for “best deal” scenarios.

ALL

All matching discounts are applied (if Shopify allows stacking for the function type).

Performance Considerations

Efficient Condition Ordering

Place simple conditions first:

Fact Reuse

Calculate once, use everywhere: Inefficient:
Efficient:

Selector Optimization

Use direct targeting when possible: Slower:
Faster:

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
Each has specific input/output schemas - see Function Types.

Metafield Access

Access custom data via metafields:

GID Handling

Shopify IDs are Global IDs (GIDs):
Function Lab handles conversion automatically:
  • 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