Functions

Markdown

View as Markdown

Functions let you return values from Storelink storefront events to change what the shopper sees and what is sent with the order, including product prices, fees, shipping rates, and checkout availability.

product:quantity-selected

Fires when the quantity or variant changes on a product page or a featured product, and for every cart row on load and whenever its quantity changes on any page.

Sample code to adjust a product's price based on quantity:

storelink.on('product:quantity-selected', function(data) {
  if (data.qty >= 5) {
    return {
      price: Math.round(data.selectedVariant.price * 0.9 * 100) / 100,
      description: '10% bulk discount applied'
    };
  }
  return {};
});

Here, the parameter data contains the product, target quantity, and selected variant.

Payload

FieldTypeDescription
productobjectFull product object.
qtynumberThe quantity the shopper would end up with: a cart row's quantity, or on a product page or featured product, what is already in the cart for that variant plus the quantity selector.
selectedVariantobjectActive variant object, including key, price, and discountedPrice. selectedVariant.price is always the base list price, never an earlier override, so tiered rules do not compound.

What You Can Return

FieldTypeDescription
pricenumberOverrides the unit price shown to the shopper.
discountedPricenumberOverrides the discounted unit price shown to the shopper.
descriptionstringOverrides the variant description shown on the product page.

How the Store Applies the Result

On the product page and featured product, returned price, discountedPrice, and description update what is displayed for the active variant. For cart rows, the item's unit price becomes discountedPrice if returned, otherwise price, and is sent with the order. When a later run returns neither price nor discountedPrice, the cart row reverts to the variant's original price.

checkout:payment-changed

Fires when the cart opens, and again whenever the payment method, subtotal, coupon, or checkout step changes.

Sample code to add a fee when Cash is selected:

storelink.on('checkout:payment-changed', function(data) {
  if (data.paymentMethod && data.paymentMethod.type === 'Cash') {
    return {
      fees: [{ name: 'COD Fee', amount: 50 }]
    };
  }
  return {};
});

Here, the parameter data contains the cart, customer, address, store, and selected payment method.

Payload

FieldTypeDescription
cartobjectCart details: items (array of { id, name, price, quantity, weight, category, tags, variant, customFields }), subtotal (number), discount (number), and appliedCoupon ({ code, type, amount } or null).
customerobjectCustomer contact fields: name, email, and phone.
addressobjectDelivery address form fields (addressLine1, addressLine2, city, state, zip, and any custom address fields).
storeobjectStore information: currency, country, and name.
paymentMethodobject or nullSelected payment method with type (for example, 'Cash', 'Stripe', 'Razorpay', 'PayPal', 'Payment Link', 'Custom') and name.

What You Can Return

FieldTypeDescription
feesarrayArray of { name: string, amount: number } objects. Each fee must have a non-empty name and a numeric amount.
shippingRatenumberReplaces the shipping cost shown in the order summary.
blockCheckoutbooleanSet to true (with blockReason) to block checkout, or false to unblock it. Once set to true, the block stays in effect until a later run returns blockCheckout: false.
blockReasonstringMessage shown to the shopper explaining why checkout is blocked. Required when blockCheckout is true; blockCheckout: true without a blockReason is ignored.

Note: Returning {} does not clear an active block. Every handler that can block checkout must return { blockCheckout: false } when the blocking condition no longer holds.

How the Store Applies the Result

  • fees are shown in the order summary, added to the total, and saved with the order. Fees are tracked per event: a run that returns no fees clears only that event's fees.

  • shippingRate replaces the shipping cost in the order summary.

  • blockCheckout: true with a blockReason disables the place-order button and displays the reason until a later run returns blockCheckout: false or the shopper returns to the cart step.

  • While handlers run, the order summary shows a loading state. If every handler for an event fails, the order summary shows an error with a retry action.

checkout:address-changed

Fires when the cart opens, and again whenever the shipping method, delivery address, subtotal, coupon, or checkout step changes.

Sample code to set a custom shipping rate based on the delivery address:

storelink.on('checkout:address-changed', function(data) {
  if (data.address && data.address.state === 'Alaska') {
    return { shippingRate: 25 };
  }
  return {};
});

Here, the parameter data contains the cart, customer, address, store, and selected shipping method.

Payload

FieldTypeDescription
cartobjectSame as in checkout:payment-changed.
customerobjectCustomer contact fields: name, email, and phone.
addressobjectDelivery address form fields (addressLine1, addressLine2, city, state, zip, and any custom address fields).
storeobjectStore information: currency, country, and name.
shippingMethodobject or nullSelected shipping method object, including id, label, and rate.

What You Can Return

The same fields as checkout:payment-changed: fees, shippingRate, blockCheckout, and blockReason.

How the Store Applies the Result

Results apply in the same way as checkout:payment-changed, with one additional rule for shipping: when a checkout:address-changed run does not return shippingRate, the shipping cost resets to the selected shipping method's rate.

How Multiple Handlers Combine

  • Execution order: Multiple handlers registered on the same event run sequentially in the order they were registered.

  • Single values: For single-value fields (price, discountedPrice, description, shippingRate, blockCheckout, and blockReason), the last handler that returns the field wins.

  • Fees: fees arrays are combined across handlers. If a later handler returns a fee with the same name as an earlier fee, the later fee replaces the earlier one.

  • Timeout and errors: Each handler has a 10-second timeout. A handler that times out or throws an error is skipped, and the remaining handlers still run and apply their results.

  • Type checks: Returned fields are type-checked. Any field with the wrong type (such as '50' instead of 50, or 'true' instead of true) is dropped.

Recipes

COD Fee

Sample code to add a Cash on Delivery fee when Cash is selected:

storelink.on('checkout:payment-changed', function(data) {
  if (data.paymentMethod && data.paymentMethod.type === 'Cash') {
    return {
      fees: [{ name: 'COD Fee', amount: 50 }]
    };
  }
  return {};
});

Minimum Order Amount

Sample code to block checkout when the cart subtotal is below a minimum amount:

storelink.on('checkout:payment-changed', function(data) {
  var minOrder = 25;
  if (data.cart.subtotal < minOrder) {
    return {
      blockCheckout: true,
      blockReason: 'Minimum order amount is ' + minOrder + ' ' + data.store.currency + '.'
    };
  }
  return { blockCheckout: false };
});

Zip Blocklist

Sample code to block checkout for specific postal codes:

storelink.on('checkout:address-changed', function(data) {
  var blockedZips = ['99501', '99502', '99503'];
  var zip = data.address && data.address.zip ? data.address.zip.trim() : '';
  if (zip && blockedZips.indexOf(zip) !== -1) {
    return {
      blockCheckout: true,
      blockReason: 'Delivery is not available for postal code ' + zip + '.'
    };
  }
  return { blockCheckout: false };
});

Heavy-Item Handling Fee

Sample code to add a handling fee when total cart weight exceeds 10:

storelink.on('checkout:address-changed', function(data) {
  var totalWeight = data.cart.items.reduce(function(sum, item) {
    return sum + (item.weight || 0) * item.quantity;
  }, 0);
  if (totalWeight > 10) {
    return {
      fees: [{ name: 'Heavy Item Handling', amount: 15 }]
    };
  }
  return {};
});

Tiered Shipping

Sample code to set shipping rates based on the cart subtotal:

storelink.on('checkout:address-changed', function(data) {
  if (data.cart.subtotal >= 100) {
    return { shippingRate: 0 };
  }
  if (data.cart.subtotal >= 50) {
    return { shippingRate: 5 };
  }
  return { shippingRate: 10 };
});

Bulk Pricing

Sample code to apply tiered unit prices based on quantity:

storelink.on('product:quantity-selected', function(data) {
  var basePrice = data.selectedVariant.price;
  if (data.qty >= 10) {
    return {
      price: Math.round(basePrice * 0.7 * 100) / 100,
      description: '30% bulk discount'
    };
  }
  if (data.qty >= 5) {
    return {
      price: Math.round(basePrice * 0.85 * 100) / 100,
      description: '15% bulk discount'
    };
  }
  return {};
});

What's Next: Continue to Page Structure.

Related Articles

Was this article helpful?