You don’t need Recharge’s widget to sell a subscription. Recharge stores every
subscription option as a Shopify selling plan, and a subscription is just
an add to cart that carries a selling_plan id. So a custom subscribe and save
selector is a Liquid snippet that lists the plans, shows the right price for the
selected variant, and puts the chosen plan id in the product form. That’s it.
I build custom selectors when the default widget doesn’t match the design, when the team wants to test the offer (default to subscribe, show savings in dollars, radio cards instead of a dropdown), or when the PDP already has enough JavaScript and we don’t want another app script on it.
How does Recharge show up in Liquid?
When a merchant sets up subscription rules in Recharge, Recharge creates selling plan groups on the product in Shopify. Each group holds one or more selling plans (“Deliver every 30 days”, “Deliver every 60 days”). Each variant then has selling plan allocations: the price of that variant under each plan, already computed by Shopify.
{% for group in product.selling_plan_groups %}
{{ group.name }}
{% for plan in group.selling_plans %}
{{ plan.id }} {%- comment -%} the value you send as selling_plan {%- endcomment -%}
{{ plan.name }} {%- comment -%} "Deliver every 30 days" {%- endcomment -%}
{% endfor %}
{% endfor %}
{% for allocation in variant.selling_plan_allocations %}
{{ allocation.selling_plan.id }}
{{ allocation.per_delivery_price | money }}
{{ allocation.compare_at_price | money }}
{% endfor %}
Two things to notice. Prices live on the variant, not the plan, because a
20% discount on a 30-count bottle and a 90-count bottle are different numbers.
And if another app also creates selling plans on the same product, check each
group’s app_id and render only Recharge’s groups.
What does the selector need to do?
Four jobs, and only one of them is about subscriptions:
- Let the customer choose one-time or subscribe.
- Let them pick a delivery frequency.
- Show the right price for the selected variant and plan.
- Put the chosen plan id in a field named
selling_plan, or leave it empty for a one-time purchase.
The fourth job is the subscription. The other three are presentation.
The markup
This goes inside the product form, next to the variant picker. Notice there is
exactly one field named selling_plan, and it’s hidden. The radios and the
dropdown have no name that Shopify reads, so they can never send a second
value.
{%- if product.selling_plan_groups.size > 0 -%}
{%- liquid
assign variant = product.selected_or_first_available_variant
assign group = product.selling_plan_groups | first
assign default_plan = group.selling_plans | first
-%}
<fieldset class="subscribe" data-subscribe data-currency="{{ cart.currency.iso_code }}">
<legend class="visually-hidden">Purchase options</legend>
{%- unless product.requires_selling_plan -%}
<label>
<input type="radio" name="purchase_option" value="onetime" checked>
One-time purchase
<span>{{ variant.price | money }}</span>
</label>
{%- endunless -%}
<label>
<input type="radio" name="purchase_option" value="subscribe"
{% if product.requires_selling_plan %}checked{% endif %}>
Subscribe and save
<span data-price-subscribe>
{%- for allocation in variant.selling_plan_allocations -%}
{%- if allocation.selling_plan.id == default_plan.id -%}
{{ allocation.per_delivery_price | money }}
{%- endif -%}
{%- endfor -%}
</span>
</label>
<select data-frequency aria-label="Delivery frequency">
{%- for plan in group.selling_plans -%}
<option value="{{ plan.id }}">{{ plan.name }}</option>
{%- endfor -%}
</select>
<input type="hidden" name="selling_plan"
value="{% if product.requires_selling_plan %}{{ default_plan.id }}{% endif %}">
{%- comment -%} Price of every variant under every plan, in cents. {%- endcomment -%}
<script type="application/json" data-subscribe-prices>
{
{%- for v in product.variants -%}
"{{ v.id }}": {
{%- for allocation in v.selling_plan_allocations -%}
"{{ allocation.selling_plan.id }}": {{ allocation.per_delivery_price }}
{%- unless forloop.last %},{% endunless -%}
{%- endfor -%}
}{%- unless forloop.last %},{% endunless -%}
{%- endfor -%}
}
</script>
</fieldset>
{%- endif -%}
Shopify ignores form fields it doesn’t know, so purchase_option is safe in
the form. The JSON block is there so the price can update when the variant
changes, without another request.
The JavaScript
The script keeps the hidden field and the price in sync with what the customer picked. It doesn’t watch the cart and it doesn’t touch prices that Shopify charges. It only reads prices Shopify already computed.
const root = document.querySelector('[data-subscribe]');
if (root) {
const form = root.closest('form');
const planInput = root.querySelector('input[name="selling_plan"]');
const frequency = root.querySelector('[data-frequency]');
const priceEl = root.querySelector('[data-price-subscribe]');
const prices = JSON.parse(root.querySelector('[data-subscribe-prices]').textContent);
const money = new Intl.NumberFormat(document.documentElement.lang || 'en', {
style: 'currency',
currency: root.dataset.currency,
});
function sync() {
const checked = root.querySelector('input[name="purchase_option"]:checked');
const subscribing = checked?.value === 'subscribe';
const variantId = form.querySelector('[name="id"]').value;
const cents = prices[variantId]?.[frequency.value];
planInput.value = subscribing ? frequency.value : '';
frequency.hidden = !subscribing;
if (cents != null) priceEl.textContent = money.format(cents / 100);
}
// Radio and frequency changes bubble up to the form. Most themes update the
// variant id on the same change event, so read it after they're done. If your
// theme updates it later, call sync() from its own variant change event.
form.addEventListener('change', () => requestAnimationFrame(sync));
sync();
}
What quietly breaks it
None of these throw an error. The customer just ends up with a one-time order, or the wrong price, and nobody notices until the subscription numbers look off.
- Two fields named
selling_plan. This usually happens when Recharge’s own widget is still enabled next to your selector, or when the dropdown also hasname="selling_plan". The browser sends both, and which one wins depends on how the theme reads the form. When you ship your own selector, turn off Recharge’s widget block or app embed in the theme editor. - An AJAX add to cart that drops the field. A lot of themes build the
/cart/add.jspayload by hand. If yours does, make sure it copiesselling_planover, and only when it has a value. - A variant with no allocation for the selected plan. Plans can differ per
variant if the Recharge rules differ. If
prices[variantId]has no entry for the plan, hide or disable the subscribe option for that variant instead of sending a plan id Shopify will reject. - A badge that overpromises. A plan’s
price_adjustmentscan include anorder_count, which means the discount only applies to the first N orders. If you print “Save 20%”, make sure it’s 20% on every delivery, not just the first one. - Prepaid plans. On prepaid plans
checkout_charge_amount(what they pay today) andper_delivery_price(what each shipment works out to) are different. Show both. - Discounts stacking on top. The selling plan price isn’t a Shopify discount, so automatic discounts and codes can apply on top of it. Shopify discounts have a purchase type setting (one-time, subscription or both). Check it on every active discount before launch.
// Your AJAX add to cart, with the plan carried over.
const data = new FormData(form);
const item = { id: Number(data.get('id')), quantity: Number(data.get('quantity') || 1) };
if (data.get('selling_plan')) item.selling_plan = Number(data.get('selling_plan'));
await fetch('/cart/add.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({ items: [item] }),
});
How I test it before launch
I add every combination to the cart (one-time, each frequency, each variant)
and read /cart.js in the console. Every subscription line should carry a
selling_plan_allocation with the plan I picked, and the price should match
what the PDP showed.
const cart = await fetch('/cart.js').then((r) => r.json());
console.table(
cart.items.map((i) => ({
item: i.title,
plan: i.selling_plan_allocation?.selling_plan?.name ?? 'one-time',
price: i.final_price / 100,
}))
);
Then I run one test order through checkout and confirm it shows up as a subscription in Recharge. If the cart is right, checkout almost always is.
The rule I follow
The selling_plan field is the subscription. Everything else in the widget is
presentation. Build it so there’s one field, one source of prices (Shopify’s),
and test that field before you style anything.
If the PDP is where most of your subscriptions start, it deserves this level of care. It’s the kind of work I do for wellness and supplement brands on a monthly retainer through MTB Labs.
FAQ
Do I need Recharge's own widget to sell subscriptions?
No. Recharge stores each subscription option as a Shopify selling plan on the product. Any add to cart that sends a valid selling_plan id with the variant creates a subscription line. Recharge's widget is one way to send that id; your own markup is another.
Why does my cart show the one-time price after I picked a subscription?
Almost always the selling_plan id never reached /cart/add. The field was empty, had the wrong name, was sent twice, or held a plan id that doesn't apply to that variant. Open /cart.js and check whether the line has a selling_plan_allocation.
How do I make a product subscription only?
Set the product as subscription only in Recharge. Shopify then reports product.requires_selling_plan as true and won't add the product to the cart without a selling plan, so your selector should hide the one-time option and preselect a plan.
Which price should I show for a prepaid subscription?
Show both. per_delivery_price is what each shipment works out to, and checkout_charge_amount is what the customer pays today. For prepaid plans those two numbers are different, and hiding the second one creates support tickets.