What is a Function?

Functions is the secret sauce of Ollie Shop. Is where the magic happens.

All Functions run server-side guaranteeing security and maximum performance.

Use functions to run the business logic of the customization. Make requests to external systems, transform payloads, go crazy.

Event-Driven Architecture

Functions operate on an event-driven model. Each function listens for specific events and executes when triggered:

Available Events

EventTriggerUse Cases
itemsCart items added/updatedInventory validation, pricing rules, upsells
cartCart data requestedDynamic pricing, tax calculations
customerCustomer info updatedProfile validation, loyalty program integration
shippingShipping method selectedRate calculations, delivery date estimation
deliveryDelivery details updatedAddress validation, delivery scheduling
paymentPayment method chosenPayment processor integration, fraud detection
extensionCustom extensions updatedThird-party service integration

Execution Timing

Configure when your function runs:

  • Before: Execute before sending data to your e-commerce platform
    • Example: Validate cart quantity limits before adding items
  • After: Execute after the platform processes the request
    • Example: Hide payment methods based on new cart total

Error Handling

Configure how your function responds to errors:

  • Throw: Abort the operation and return a 4xx error to the user
  • Skip: Continue processing without raising errors

Use the Ollie SDK’s Reject function to return structured error responses:

import { reject } from "@ollieshop/sdk";

// Return a 422 error with custom message
return reject("Maximum quantity exceeded for this product");

Function Requirements

Functions follow the same structure as Components:

Required Files

  1. TypeScript file - Your function logic
  2. package.json - Dependencies and metadata
  3. node_modules/ - Installed dependencies
Functions don’t require CSS files since they run server-side

Basic Function Structure

import { type OllieFunction } from "@ollieshop/sdk";

const validateQuantity: OllieFunction = async ({ cart, event }) => {
  // Your business logic here
  if (cart.items.some(item => item.quantity > 10)) {
    throw new Error("Maximum 10 items per product");
  }
  
  return cart; // Return modified cart or original
};

export default validateQuantity;

Deployment

Deploy functions using the Ollie Builder API:

  1. Compress your function folder as a ZIP file
  2. Use the API with your function ID
  3. Associate with the appropriate Version

Development Guide

Learn how to build and deploy Functions step-by-step