appstle-subscription.js, which loads on your storefront through the Appstle Subscriptions app embed. You can listen to these events to track subscription interactions in analytics tools, show or hide custom UI, and react to customer actions in the cart and portal.
Unless a table row says otherwise, events are dispatched on both document and window, so you can attach listeners to either.
These events exist only on a Shopify theme where the Appstle Subscriptions app embed is on (in the theme editor, under App embeds). A headless storefront doesn’t load
appstle-subscription.js, so nothing fires them there. Once the embed is on, you can listen without any other setup.Subscription widget events
These events fire when customers interact with the subscription widget on product pages.
A widget ID is the widget’s position on the page, starting at
0 for the first widget. It’s either event.detail itself or its widgetId field. Because it can be 0, check it with typeof, for example typeof event.detail === 'number', not with if (event.detail).
trigger says how the add-to-cart was sent:
In this table,
/cart/add means any URL that contains it, such as /cart/add.js or /fr/cart/add.
All triggers except form_submit and appstle_add_to_cart come from the store’s add-to-cart interceptor. They fire just before the request is sent, and only while the interceptor is on. To learn what the interceptor does and who can turn it off, read When the Appstle app embed is on.
One add-to-cart can fire AddToCartIntent more than once. Count these as one add:
- A theme that submits the product form with
fetchorXMLHttpRequestfiresform_submitand that request’s trigger, such asfetch_form_data.form_submitcan arrive after the theme has sent its request. - A
jquery_ajaxadd also firesxml_http_request.
Theme product form
The widget puts the customer’s choice in your theme’s product form, as theselling_plan value that Add to cart sends to Shopify: the selling plan ID, or an empty string for a one-time purchase. When the widget is inside the form, the value comes from the purchase option the customer checked. When the widget is outside the form, the widget adds a hidden selling_plan field to the form and keeps it up to date.
When the customer changes the choice, the widget updates the form first and then fires sellingPlanChanged. Once the widget has loaded, the form holds the current choice, including a plan from a ?selling_plan= URL parameter. event.detail holds only the plan. To get the variant with it, read the form in your sellingPlanChanged listener:
- Read the value with
FormData, not from one field. The form can hold more than one field namedselling_plan: the widget adds a disabled hidden one for Shopify’s Buy it now button, and an option that isn’t selected can have an empty value.FormDataholds only what Add to cart sends, soget('selling_plan')returns the customer’s choice. Build on the value, not on the fields: the fields are the widget’s markup and can change, as the warning at the top of this page says. - Read the variant from the form’s
idfield on every event. When the customer changes the variant, the first events can fire before your theme updates that field. The widget fires the event again after the field changes, so the last event has the new variant with its plan. Don’t keep a variant from an earlier event. For the listener rules, see Keep custom content in sync with the selected plan. - On a page with more than one widget, read every form your content depends on.
sellingPlanChangeddoesn’t say which widget fired it. On each event, read each product form whose choice your content shows.readPurchaseChoice()reads the first one, so change its lookup to reach another.
Theme product price
When a customer picks a subscription with a discount, the widget also changes the product price that your theme shows, so the subscription price appears where customers already look. Appstle support sets which element is the theme price for each store. If the theme price doesn’t change when a customer picks a plan with a discount, or another price on the page changes too, ask Appstle support to check the store’s price element. On every update, the widget changes the theme price like this:
With a subscription selected, the theme’s price group looks like this. The copy takes the tag of your theme’s price element, a
span here:
Use these selectors only for styling, and take prices from Shopify as described in Keep custom content in sync with the selected plan. The selectors are part of the widget’s markup, so the warning at the top of this page applies: check your CSS after a widget update. The widget removes the copy and the badge and builds them again on every update, so change them with CSS, not with a script.
To hide the crossed-out original, match the element that follows the copy. The copy exists only while the original is crossed out, so the rule never leaves the product without a visible price:
variant.compare_at_price in Liquid, or the variant’s compare_at_price in the product’s JSON. Don’t use the selling plan allocation’s compare_at_price for this. It’s the variant’s price without the plan’s discount, not the variant’s compare-at price.
The widget can also change the theme’s unit price and the add-to-cart button’s text, when they’re set up for the store. Ask Appstle support to set them up. The button’s text while a subscription is selected comes from the widget settings and can include the subscription price. The widget doesn’t change other prices on the page, such as one in a variant picker or a sticky add-to-cart bar, unless that price matches the store’s price element. To keep those in step, listen to sellingPlanChanged and read the prices from the product’s selling plan allocations, as in Show a price summary that follows the selected plan.
Cart widget events
Customer portal events
To put the portal on one of your pages and start it from your code, see Embed the customer portal on a page.Build-a-Box events
Other events
Code examples
Track subscription selections in analytics
Show custom UI when the widget loads
React to cart subscription changes
Run custom logic around customer portal embedding
Keep custom content in sync with the selected plan
Follow these rules for any content of your own that changes with the customer’s choice, such as a badge, a message or a price.- Listen to
sellingPlanChanged. Itsevent.detailis the current choice. It fires as the widget loads, when the customer picks an option, and after a variant or quantity change, so one listener covers every case. - Attach the listener before the widget loads. Put the script inline in the product section or in a Custom Liquid block. A listener that’s added later misses the events fired while the widget loads.
- Expect repeats. The event can fire several times in a row, sometimes with the same value. Render from
event.detaileach time and don’t count events. - Don’t depend on the order of events. When a customer selects a plan,
SellingPlanDeSelectedfires first, thenSellingPlanSelected, andSubscriptionWidgetUpdatedfires both before and aftersellingPlanChanged. - Take prices from Shopify, not from the widget’s markup. Show a price summary that follows the selected plan reads them from the product’s selling plan allocations.
Show a price summary that follows the selected plan
Use this to show your own price line, for example under the product title, that follows the customer’s choice. The widget’s events don’t carry prices, so the script fetches the product’s JSON from Shopify and reads each variant’s selling plan allocations. Put it in a Custom Liquid block in the product section: the listener must exist before the widget loads, and the block fills in the product’s handle.- The script reads the current variant from the
idfield of the theme’s product form, the field that tells the cart which variant to add. - Each variant’s
selling_plan_allocationshas one entry for each plan the variant is sold with.priceis the price with the plan, andper_delivery_priceis the price of one delivery. They differ only when the customer pays for several deliveries up front. Thenpriceis what the customer pays at checkout, so the script shows both. A try-before-you-buy plan charges nothing at checkout, and a preorder plan can charge only a deposit. To show those amounts, readcheckout_charge_amountin Liquid, as in Showing plan prices in Liquid.compare_at_priceis whatpricewould be without the plan’s discount, ornullwhen the plan has no discount. - Shopify gives prices as the amount times 100, such as
995for $9.95. It does this in every currency, including currencies without cents such as JPY, so the script always divides by 100. - When the customer changes the variant, the event fires several times. The first ones can come before your theme updates the
idfield, so the summary can show the previous variant for a moment. The widget fires the event again after the field changes, so the summary ends on the new variant.
Pass the selected plan to a custom add-to-cart button
Use this when your own button adds items to the cart, for example a “Build your bundle” button, and the items must carry the purchase option the customer picked in the Appstle widget. The event repeats the current choice many times, so keep the latestevent.detail instead of reacting to each event, and add selling_plan to each item only when it isn’t empty.
Put the script inline on the product page, in a <script> tag in the product section or in a Custom Liquid block, so the listener exists before the widget loads.
event.detail doesn’t say which widget sent it. On a page with more than one Appstle widget, the script keeps the choice from the widget that fired last.
addItemsWithSelectedPlan() from your button’s click handler, with an array of variant IDs.
The cart answers 422 when an item doesn’t fit the customer’s choice, and the function throws the cart’s description:
Cannot apply selling plan to variant: a variant you add isn’t offered with the selected plan.Variant can only be purchased with a selling plan.: the customer chose one-time purchase, and a variant you add requires a plan.
Next steps
Build your own frontend
Selling plans, the cart and the customer portal from your own UI.
API changelog
Dated changes to these events and the Subscriptions APIs, with what to do about each.
Webhooks
React to new subscriptions on your server.
Authentication
Admin API keys and how storefront calls are authenticated.