1

I have just started using Craft Commerce (only Lite version currently) and I'm currently trying to get to grips with what is possible. My question is regarding subscriptions.

The site that I'm working on needs to support subscriptions. It has a single, flat rate plan which would charge users monthly. Not only that but it requires that once a user/guest has subscribed to a plan, they are then registered as Craft users. So far I have set up the plan in Stripe and in Craft using the standard template below.

{% set plans = craft.commerce.getPlans().getAllPlans() %}

{% for plan in plans %}
<form method="post">
    <input type="hidden" name="action" value="commerce/subscriptions/subscribe">
    <input type="hidden" name="planUid" value="{{ plan.uid|hash }}">
    {{ csrfInput() }}

    <h4>{{ plan.name }}</h4>

    {% set paymentSources = craft.commerce.
        getPaymentSources().
        getAllGatewayPaymentSourcesByUserId(
            plan.getGateway().id,
            currentUser.id ?? null
        )
    %}

    {# if we don’t have a saved payment soruce, collect details for the gateway #}
    {% if not paymentSources|length %}
        <div class="paymentForm">
            {{ plan.getGateway().getPaymentFormHtml({})|raw }}
        </div>
    {% endif %}

    <button type="submit">{{ "Subscribe"|t }}</button>
</form>
{% endfor %}

When I try to submit this form it simply reloads the current page. Nothing is recorded in Stripe or Craft. Not too sure why this would be.

Ideally what I'd like is for a user to subscribe to a plan and simultaneously register themselves as a user on the site, so that they would be able to log in and out. Is Commerce able to support user registration when purchasing a subscription?

1

This can definitely be done, but I don't think it's possible to do it all in the same form without using a custom module to handle either creating the user programmatically and then submitting to the Commerce controller or vice versa.

Craft has support for creating a user account via a front-end form (see the User Registration Form in the Craft docs) but you can't submit to that controller and also submit to the Commerce subscriptions controller as per the standard example template at the same time.

It might be possible to achieve something close to what you're looking for by showing a user registration form first, and then set the redirect input for that form to the payment page. However, if you want to have the user logged in directly after registration but before payment in order to save the user's payment details etc., you're going to need to do some custom development work to log the user in programmatically first since the default user registration controller just creates the account but doesn't log the user in.

I've done this sort of process where the user account is created, then logged in automatically, then they enter their billing address and payment details, but that was using Commerce Pro and a multi-step registration/payment form which is a bit more complex than your use case.

| improve this answer | |
1

Commerce needs an actual user for subscriptions but you can make onboarding pretty simple by doing a couple things:

  • Uncheck "Verify email addresses" in the CP under Settings -> Users -> Settings.
  • Set deferPublicRegistrationPassword to true so you don't need the user to set a password
  • Set the autoLoginAfterAccountActivation config to true. This, along with skipping email verification, makes it possible to use the same session.
  • Set useEmailAsUsername to true. Does what it says; sets the username to the email address.
        'useEmailAsUsername' => true,
        'deferPublicRegistrationPassword' => true,
        'autoLoginAfterAccountActivation' => true
        

The user will be sent the "activate your account" email so you might want to customize that a bit to say something like "Activate your account and set a password so you can login to manage your subscription."

If they don't set a password, they'll need to go through the motions of doing a password reset to manage their account.

That will allow you to simply grab the email email address on the form. You could do that via ajax on the same form (say after the users tabs off) or redirect back to the same form using something like:

{% if not currentUser %}
   {# show the registration form #}
{% else %}
    {# show the plans #}
{%endif%}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.