Skip to main content

Shopify Rust Functions Guide

Overview

Shopify Functions run on Shopify’s infrastructure as WebAssembly (WASM) modules. They must be written in Rust using the shopify_function crate.

Function Structure

Basic Discount Function

Function Types

1. Product Discount Function

Target: purchase.product-discount.run Use Case: Apply discounts to specific products/variants

2. Order Discount Function

Target: purchase.order-discount.run Use Case: Apply discounts to entire order

3. Shipping Discount Function

Target: purchase.shipping-discount.run Use Case: Modify shipping rates

4. Payment Customization

Target: purchase.payment-customization.run Use Case: Hide/rename payment methods

5. Delivery Customization

Target: purchase.delivery-customization.run Use Case: Modify delivery options

6. Cart/Checkout Validation

Target: purchase.validation.run Use Case: Block checkout based on conditions

Input Query Examples

Discount Function Input

Payment Customization Input

Cargo.toml Template

Extension TOML Template

Common Patterns

1. Percentage Discount

2. Fixed Amount Discount

3. Conditional Logic

4. Customer Segment Checking

Deployment Flow

  1. Generate Rust Code: AI generates src/run.rs
  2. Create GraphQL Query: Define input schema in src/run.graphql
  3. Add Cargo.toml: Rust package configuration
  4. GitHub Actions:
    • Installs Rust toolchain
    • Compiles to wasm32-wasi target
    • Optimizes with wasm-opt
  5. Shopify CLI: Deploys WASM module to Shopify

Testing Locally

Best Practices

  1. Keep Functions Fast: < 10ms execution time
  2. Handle Missing Data: Use .unwrap_or() and error handling
  3. Validate Input: Check configuration and cart data
  4. Clear Messages: Provide descriptive discount messages
  5. Test Edge Cases: Empty cart, zero prices, missing fields

Resources