🛒 AutoCart Docs

Discount Function Extension

Server-side Shopify Function that applies product discounts to autocart items.

Overview

The Discount Function extension runs server-side on Shopify's infrastructure to apply product-level discounts to cart items that were added by AutoCart.

How It Works

  1. When AutoCart adds a product to the cart, it attaches line item properties to that specific cart item containing the discount configuration
  2. At checkout, Shopify invokes the discount function with the current cart state (including all line item properties)
  3. The function scans each cart line's properties, looking for the AutoCart discount property
  4. When found, it reads the discount configuration and returns the appropriate discount allocation for that item

Line Item Properties

When AutoCart adds a product to the cart, it attaches several line item properties to that cart line. These properties are used internally by AutoCart and the discount function to identify and manage autocart items.

All Properties

Property KeyValuePurpose
_autocartRule title (string)Identifies this item as an AutoCart-managed item. Used to detect autocart items vs. customer-added items.
_titleRule title (string)The title of the automation rule that added this item. Used for matching items to rules during re-evaluation and cleanup.
_autocart_discount_functionJSON string (see below)Contains the discount configuration. Read by the Discount Function to determine what discount to apply. Set to "null" for code-based discounts.
_scope"global", "checkout", "storefront", or "remove"The scope of the rule. Determines where the rule is evaluated.

Line item properties starting with _ are hidden from the customer in the cart and checkout UI, so all AutoCart metadata is invisible to shoppers.

_autocart_discount_function Format

The _autocart_discount_function property contains a JSON string with the discount configuration and the list of product entries:

{
  "discount": {
    "variant": "automatic",
    "active": true,
    "pressedButton": "percentage",
    "value": "100"
  },
  "products": [
    { "id": "gid://shopify/ProductVariant/12345", "quantity": 1, "handle": "product-handle" }
  ]
}

Discount Fields

FieldDescription
variantThe discount type — "automatic" (applied by the discount function) or "code" (applied via discount code)
activeWhether the discount is active
pressedButtonThe discount method — "percentage" or "fixed"
valueThe discount amount — e.g. "100" for 100% off (free gift), "50" for 50% off

When the rule uses a code-based discount (variant: "code"), the _autocart_discount_function value is set to "null". The discount code is applied separately and not processed by the discount function.

Free Gift Detection

A "free gift" is identified when:

  • The discount variant is not "code"
  • The discount is active
  • The type is "percentage"
  • The value is "100"

Free gifts are added immediately to the cart (skipping the confirmation step) because there is no price impact for the customer.

Code Discounts

When a rule uses a code-based discount, the discount attribute is not set. Instead, the discount code is applied separately via the checkout API. The discount function does not process code-based discounts.

Configuration

The discount function is configured through the AutoCart app interface. Both the create and detail views for the discount are managed through the main app settings. No separate configuration is needed — the function reads the attributes automatically from cart line items.

On this page